Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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.

Jeremy

Comment on Mistakenly Believing split is using @_
Select or Download Code
Replies are listed 'Best First'.
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:
    split;
    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.

    MM

    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 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.

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";

    ~Particle

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2015-07-29 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls