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_
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.)
Let diagnostics provide more information:
use strict;
use warnings;
use diagnostics; # comment this out when you are done debugging
my $s = "aaa bbb";
my $num_of_item = split(/\s+/, $s) ;
print $num_of_item;
Use of implicit split to @_ is deprecated
(D deprecated, W syntax) It makes a lot of work for the compiler when you clobber a subroutine's argument list, so it's better if you assign the results of a split() explicitly to an array (or list).
A better way to get diagnostic info is from the command line:
perl -Mdiagnostics my_program.pl
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).