我有这个奇怪的问题,因为它不会默认拆分为默认数组.
下面是一些玩具代码.
#!/usr/bin/perl $A="A:B:C:D"; split (":",$A); print $_[0];
这不会打印任何东西.但是,如果我明确拆分为默认数组,如
#!/usr/bin/perl $A="A:B:C:D"; @_=split (":",$A); print $_[0];
它正确打印A.我的perl版本是v5.22.1.
解决方法
默认情况下,
split
不会转到@_. @_不像$_那样工作.它只适用于函数的参数.
perlvar说:
Within a subroutine the array @_ contains the parameters passed to that subroutine. Inside a subroutine,@_ is the default array for the array operators pop and shift.
如果你运行你的程序使用严格和使用警告,你会看到
Useless use of split in void context at
但是,如果没有提供任何内容,split会使用$_作为其第二个参数(拆分的字符串).但是你必须始终使用返回值.