Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Mistakenly Believing split is using @_

by enoch (Chaplain)
on Sep 19, 2001 at 22:05 UTC ( #113425=perlquestion: print w/ replies, xml ) Need Help??
enoch has asked for the wisdom of the Perl Monks concerning the following question:

Perl gives the warning: Use of implicit split to @_ is deprecated when used in the following code:
print scalar((split('', "I am a string"))) . "\n";
Obviously, I am not making use of @_ in this split. Is perl warning because I am not assigning the return value of the split to an array? Because if I do this:
my @chars = split '', "I am a string"; my $num = scalar(@chars); print $num . "\n";
It does not warn. I am not really worried about the warning because the output ('13' in this case) is correct. I was just wondering if this is a bug, or if this is by design. And, if it is by design, why? Or, perhaps somewhere in the parsing, perl becomes confused with the scalar call on the returned array of split. I don't know. I am just wondering. I have tried this on both perl v5.6.1 for i686-linux and ActiveState perl v5.6.0 binary build 623 and received the same warning.


Comment on Mistakenly Believing split is using @_
Select or Download Code
Re: Mistakenly Believing split is using @_
by Maestro_007 (Hermit) on Sep 19, 2001 at 22:15 UTC
    It's doesn't warn as a result of what you're splitting on, but what you're assigning to. In the olden days, the idiom would look like this:
    which would automatically assign the contents to @_. Maybe the warning should say Implicitly assigning result of split() to @_ is deprecated. I got burned on that one once upon a time, myself.

    The reason the second snippet doesn't warn is that you have assigned the result to @chars. I think it's safe to say that the newer behavior is a Good Thing.

    As to why the warning doesn't understand that you are still using the results in another context is a little puzzling, something that merlyn can probably answer.


    Update: void context =~ scalar context, got it. Thanks japhy!

Re: Mistakenly Believing split is using @_
by japhy (Canon) on Sep 19, 2001 at 22:15 UTC
    split() in scalar context (update: void context is a special type of scalar context) assigns to @_. That's documented. Chances are you can use something other than split() if you just want a field-count.

    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Mistakenly Believing split is using @_
by Anonymous Monk on Sep 19, 2001 at 22:18 UTC
    If you are trying to get the number of characters in a string you should use length instead of split('').
Re: Mistakenly Believing split is using @_
by particle (Vicar) on Sep 19, 2001 at 22:20 UTC
    i believe you are using the depricated feature.

    if you don't set the value of split to an array, such as my @chars=split '', "I am a string";, then it's set to @_.

    { print scalar(my @chars=(split('', "I am a string"))) . "\n"; }
    works fine. the scoping should eliminate the @chars var when you're done with it.

    although, you might try length as in

    print length("I am a string"), "\n";


Re: Mistakenly Believing split is using @_
by lemming (Priest) on Sep 19, 2001 at 22:22 UTC

    From perldoc perlfunc:
    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.

    It's preventing you from possibly shooting yourself in the foot.

    For a better method, use the function length() to count the number of characters in a string.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://113425]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-08-01 01:13 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (256 votes), past polls