samizdat has asked for the wisdom of the Perl Monks concerning the following question:

Goodly monks - In my sort functions, I use split(//,$a) to get length of string $a, etc. I am getting a complaint from 'use strict' as follows:
Use of implicit split to @_ is deprecated at ./spiceparam.pl line 665. Use of implicit split to @_ is deprecated at ./spiceparam.pl line 666.
Here is the offending sub (which was shorter originally):
sub by_value_length { my $sa = split(//,$vartable{$a}); my $sb = split(//,$vartable{$b}); ($sa <=> $sb) || ($vartable{$a} cmp $vartable{$b}) || ($a cmp $b); }
Can someone please explain my offense? TIA!!! :D

Replies are listed 'Best First'.
Re: Implicit split to @_
by tlm (Prior) on Apr 14, 2005 at 16:28 UTC

    Just use the length function:

    my $sa = length( $a ); my $sb = length( $b );

    the lowliest monk

      gaak! /me {red}. Thanks, PP, Jaap, crashtest!!!
Re: Implicit split to @_
by crashtest (Curate) on Apr 14, 2005 at 16:36 UTC
    From the documentation for split:
    In scalar context, returns the number of fields found and splits into the @_ array. Use of split in scalar context is deprecated, however, because it clobbers your subroutine arguments.
    And the output I get from use diagnostics:
    Use of implicit split to @_ is deprecated at test.pl line 7 (#1). (D deprecated) 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).
Re: Implicit split to @_
by gellyfish (Monsignor) on Apr 14, 2005 at 16:47 UTC

    If you want to do it like that for whatever reason you may have (rather than using length as others have suggested) you will need to use a temporary array:

    my $sa = do { my @tmp = split //, $vartable{$a} };
    The do block is there just to reduce the scope of the temporary array - you can do away with it if you want to predeclare the array.

    Of course I would recommend that you do this with length

    /J\

      my $sa = () = split //, $vartable{$a}, -1 also does the trick. (Update: added necessary -1.)

        Er are you sure:

        perl -e 'my $sa = () = split //,"foo"; print $sa'
        vs
        perl -e 'my $sa = do {my @tmp = split //,"foo"}; print $sa'
        split() is outwitting us here - it sees the empty list and optimizes accordingly.

        /J\

Re: Implicit split to @_
by Jaap (Curate) on Apr 14, 2005 at 16:29 UTC
    I assume it is because you call split in scalar context.
    You could also use my $sa = length($vartable{$a});