Why does Perl complain “Use of implicit split to @_ is deprecated”?

前端 未结 3 1281
猫巷女王i
猫巷女王i 2020-12-05 17:49

This code triggers the complaint below:

#!/usr/bin/perl 
use strict;
use warnings;

my $s = \"aaa bbb\";
my $num_of_item = split(/\\s+/, $s) ;
print $num_of_         


        
3条回答
  •  隐瞒了意图╮
    2020-12-05 18:32

    From the split docs:

    In scalar context, returns the number of fields found. In scalar and void context it splits into the @_ array. Use of split in scalar and void context is deprecated, however, because it clobbers your subroutine arguments.

    So, since you're using it in scalar context, it splits into the @_ array, which is a deprecated usage. (It has to do the split though, since it'd break old code expecting it to split into @_ - no way around the warning without assigning into a temporary array, as far as I know. Eugene Y has this explicitly in his answer.)

提交回复
热议问题