Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Stumped when passing barewords

by broquaint (Abbot)
on Apr 22, 2004 at 13:04 UTC ( #347339=note: print w/ replies, xml ) Need Help??


in reply to Stumped when passing barewords

This is purely conjectural but, because at the point of compilation in sub1 the prototype of sub2 has yet to be defined, the args will be passed in as is. However, at the point when sub3 is compiled the prototype has been seen, so the args are munged to fit the prototype so @_ is forced into a scalar context. So if we predeclare sub2 we see consistent behaviour of calling sub2 with @_ e.g

sub sub2(*); sub sub1(*) { print "args(1): @_\n"; sub2(@_); } sub sub2(*) { print "args(2): @_\n"; } sub sub3(*) { print "args(3): @_\n"; sub2(@_); } sub1 Bareword; sub3 Bareword; __output__ args(1): Bareword args(2): 1 args(3): Bareword args(2): 1
Although I'm not positive on that explanation, I believe it's something along those lines.

Update: indeed, if we change the prototype between the compilation of the subs we see the output changes to the expected behaviour of the above explanation e.g

sub sub2(*); sub sub1(*) { print "args(1): @_\n"; sub2(@_); } sub sub2 { print "args(2): @_\n"; } sub sub3(*) { print "args(3): @_\n"; sub2(@_); } sub1 Bareword; sub3 Bareword; __output__ args(1): Bareword args(2): 1 args(3): Bareword args(2): Bareword
HTH

_________
broquaint


Comment on Re: Stumped when passing barewords
Select or Download Code
Re: Re: Stumped when passing barewords
by Fletch (Chancellor) on Apr 22, 2004 at 13:23 UTC

    Yes, if you read perlsub on prototypes carefully it would appear that "*" forces scalar context in the same way that "$" would. Might be worth someone working up an extra sentence which makes an explicit mention of the fact and submitting a patch to p5p.

    Update: And I've just submitted said doc patch.

      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?

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-12-21 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls