问题描述
此代码触发以下投诉:
#!/usr/bin/perl
use strict;
use warnings;
my $s = "aaa bbb";
my $num_of_item = split(/\s+/, $s) ;
print $num_of_item;
运行代码时,Perl抱怨"Use of implicit split to @_ is deprecated
".我真的没有问题的背景",所以我希望您能帮助您解释问题所在代码.
When I run the code, Perl complains that "Use of implicit split to @_ is deprecated
" .I really have no "context" for the problem, so I expect you help to explain what's wrong withthe code.
推荐答案
您在标量上下文中使用split
,并且在标量上下文中将其拆分为@_
数组. Perl警告您,您可能刚刚破坏了@_. (有关更多信息,请参见 perldoc拆分.)
You are using split
in scalar context, and in scalar context it splits into the @_
array. Perl is warning you that you may have just clobbered @_. (See perldoc split for more information.)
要获取字段数,请使用以下代码:
To get the number of fields, use this code:
my @items = split(/\s+/, $s);
my $num_of_item = @items;
或
my $num_of_item = () = split /\s+/, $s, -1;
注意:split()的三参数形式是必需的,因为如果不指定限制,split只会分割出一个片段(超出了其中的一个)作业).
Note: The three-argument form of split() is necessary because without specifying a limit, split would only split off one piece (one more than is needed inthe assignment).
这篇关于为什么Perl抱怨“不建议使用对@_的隐式拆分"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!