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

前端 未结 3 1285
猫巷女王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:49

    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;
    

    or

    my $num_of_item = () = split /\s+/, $s, -1;
    

    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 in the assignment).

提交回复
热议问题