Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Context propagation into subs and evals

by moritz (Cardinal)
on Feb 21, 2012 at 09:59 UTC ( #955261=note: print w/replies, xml ) Need Help??


in reply to Context propagation into subs and evals

It seems that what you wrote is correct. A possible source of confusion is that some part of the context is partially determined at compile time, and partially at run time.

In particular, the void context of all lines except the last inside a subroutine is determined at compile time, and the "Useless use of ... in void context" warnings from use warnings or -w are based on that:

$ perl -wce 'sub f { 1 + 1; 1}; f' Useless use of a constant (2) in void context at -e line 1. -e syntax OK

At run time, the context of the last expression or the return value is evaluated in the same context as the the whole sub, and that context information recursively flows inwards into any subs. So if you have

sub f() { g() }

then the context of g is that of f, determined dynamically at run time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://955261]
help
Chatterbox?
[marto]: the fun part, I had no fun this weekend, very stressful :)
[Corion]: But I feel an introductory talk gestating, working title "Reading CPAN" - how to read module documentation, how to judge a module, how to read the module tests/examples
[Corion]: marto: Ouch :-/ The kids are making trouble?
[Corion]: Meh - food becons, and colleagues are staring at me, talk to you later, sorry!
[marto]: Corion I've been toying with this as a background task

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2017-08-21 09:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (319 votes). Check out past polls.

    Notices?