Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Scalar context changes bareword value?

by crenz (Priest)
on Apr 22, 2004 at 13:27 UTC ( #347345=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Stumped when passing barewords
in thread Stumped when passing barewords

broquaint, thanks for the research. It seems you found the phenomenon causing the problem.

Fletch, I would expect for the bareword to stay the same, even in scalar context. If I change sub 1 to

sub sub1(*) { print "args(1): @_\n"; print scalar $_[0], "\n"; sub2(@_); }

the second value printed is Bareword and not 1.

So am I misunderstanding the spec here, or is it a bug?

Replies are listed 'Best First'.
Re: Scalar context changes bareword value?
by Fletch (Chancellor) on Apr 22, 2004 at 13:30 UTC

    The bareword is the same in scalar context, however the array @_ in scalar context is another matter. @_ itself hasn't changed, but when you pass it to sub2 it's put in scalar context ("*" is behaving similarly to "$" in this case).

      Thanks for the explanation! I can just pass $_[0] in this situation, which should make it work. However, if there would be multiple parameters and I wanted to pass @_ as a whole, there'd be a problem :(.

        Well, not a problem but you'd have to call it as sub2( $_[0]  => @_[1..$#_] ). Then again, you play with prototypes you're going to get burned. Or was that matches . . .

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347345]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-03-23 04:28 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (281 votes). Check out past polls.