Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

We had a confused discussion at last Darmstadt Perlmongers meeting, about how the callers context of a sub effects the context within the sub.

Side question was how to reliably force void-context.

Though the perldocs of return could be clearer, I came up with basic rules I wanna share and discuss here:

1. Only returned statements/lines have the callers context.

2. All other lines are per default in void context.

3. Implicitly returned statements - i.e. final but without return keyword - are like in case 1 also in callers context.

The last point can be sometimes confusing, because with nested control flow it's not always instantly clear where a sub possibly drops out.

Please see the following testcode and output for deeper understanding and as a base for your own tests.

Questions, discussions and modifications are welcome.

# -------------------- # force context # -------------------- sub VOID (&) { print "VOID:\t"; $_[0]->(); return; } sub LIST (&) { print "LIST:\t"; () = $_[0]->(); return; } sub SCALAR (&) { print "SCALAR:\t"; scalar $_[0]->(); return; } # -------------------- # tests # -------------------- sub tst_context { unless (defined wantarray) { print "is void\n"; } elsif (wantarray) { print "is list\n"; } else { print "is scalar\n"; } } SCALAR { tst_context() }; LIST { tst_context() }; VOID { tst_context() }; # -------------------- # implicit_returns # -------------------- sub implicit_returns { print "implicit_returns(@_)\t"; if ($_[0]) { tst_context() # caller's context } else { tst_context() # caller's context } } SCALAR { implicit_returns(1) }; SCALAR { implicit_returns(0) }; LIST { implicit_returns(1) }; LIST { implicit_returns(0) }; VOID { implicit_returns(1) }; VOID { implicit_returns(0) }; # -------------------- # no_implicit_returns # -------------------- sub no_implicit_returns { print "no_implicit_returns(@_)\t"; if ($_[0]) { tst_context() # void context } else { tst_context() # void context } return; # last execution! } SCALAR { no_implicit_returns(1) }; SCALAR { no_implicit_returns(0) }; LIST { no_implicit_returns(1) }; LIST { no_implicit_returns(0) }; VOID { no_implicit_returns(1) }; VOID { no_implicit_returns(0) };


SCALAR: is scalar LIST: is list VOID: is void SCALAR: implicit_returns(1) is scalar SCALAR: implicit_returns(0) is scalar LIST: implicit_returns(1) is list LIST: implicit_returns(0) is list VOID: implicit_returns(1) is void VOID: implicit_returns(0) is void SCALAR: no_implicit_returns(1) is void SCALAR: no_implicit_returns(0) is void LIST: no_implicit_returns(1) is void LIST: no_implicit_returns(0) is void VOID: no_implicit_returns(1) is void VOID: no_implicit_returns(0) is void

UPDATE: same rules apply to evals:

# -------------------- # eval # -------------------- LIST { eval "print 'eval '; return tst_context(); 'never executed'" }; #-> LIST: eval is list

Cheers Rolf

In reply to Context propagation into subs and evals by LanX

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [shmem]: a pupal bug then. Sits there in dev and hatches in production.

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (13)
    As of 2018-02-20 13:54 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (271 votes). Check out past polls.