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

I am getting the following warnings. I understand why they are appearing ( Use of implicit split to @_ is deprecated ) but I'm not sure what the best way to rewrite the code. Suggestions?

Use of implicit split to @_ is deprecated at ./tie-listkeyshash-test.p +l line 31. Use of implicit split to @_ is deprecated at ./tie-listkeyshash-test.p +l line 32.
And the code:
my @paths = ( "C:\\foo\\bar", "C:\\foo\\bar\\baz", "C:\\car", ); print "paths", Dumper(\@paths); @paths = sort { scalar (split (/\\/ , $b)) <=> scalar (split (/\\/ , $a)) } @paths; print "paths", Dumper(\@paths);

Update: I just did the following:
sub dir_component_count($) { my $path = shift; my @components = split /\\/ , $path; return scalar (@components); } @paths = sort { dir_component_count($b) <=> dir_component_count($a) } @paths;

Replies are listed 'Best First'.
Re: Sort path strings by component count.
by Roy Johnson (Monsignor) on Feb 24, 2006 at 18:45 UTC
    Don't use split to count:
    @paths = sort { $b =~ y/\\// <=> $a =~ y/\\// } @paths;

    Caution: Contents may have been coded under pressure.
Re: Sort path strings by component count.
by ikegami (Patriarch) on Feb 24, 2006 at 18:49 UTC
    Why use split at all if you only want to count the components? Just count the slashes using tr///, as follows:
    @paths = sort { $b =~ tr/\\// <=> $a =~ tr/\\// } @paths;

    By the way, <=> will force a scalar context, so scalar is not needed.

Re: Sort path strings by component count.
by doom (Deacon) on Feb 25, 2006 at 01:05 UTC
    I fixed something like this the other day just by giving it an explict array to split to. The original code was something like:
    my $word_count = int(split(' ', $string));
    And I replaced it with:
    my $word_count = scalar(my @words = split(' ', $string));
    My first thought was that using "split" to count was inelegant, but there were two problems with getting away from it in this case.
    One is that perl's m// doesn't give you a count of matches, and using s/// to count without changing the input $string would require something which is also rather inelegant:
    $string =~ s/(\s+)/$1/g;
    The other problem was that it's not so simple to precisely match split's ' ' special case, which ignores any leading or trailing spaces.
    On balance, I thought that introducing a spurious array was my best option.
Re: Sort path strings by component count.
by kaif (Friar) on Feb 25, 2006 at 21:03 UTC
    Would defining
    sub lenkf { return scalar @_ }
    and using lenkf split .. instead of scalar split .. work?