Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

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

At compile-time, the context is set to one of the following:

  • Void
  • Scalar
  • List
  • From subroutine's caller

An operator cannot control context at run-time. It cannot be any other way because operators are executed after their operands. Consider

@x = ( f(), g(), h() );

There are four ops on the RHS of the assignment. In the order they are called, they are:

  1. entersub(f): Calls f and leaves result on the stack
  2. entersub(g): Calls g and leaves result on the stack
  3. entersub(h): Calls h and leaves result on the stack
  4. list: Filters all but the last element off the stack in scalar context. No-op otherwise

The list operator never has a chance to control the context of its operands since they've already been evaluated by the time the list operator is evaluated.

That is actually the source of a known bug in Perl. What follows explains it. At compile-time, the contexts in which the above operators are evaluated are set as follows:

  1. entersub(f): List
  2. entersub(g): List
  3. entersub(h): List
  4. list: List [*]

And it works well. It also works well in scalar context:

$x = ( f(), g(), h() );
  1. entersub(f): Void
  2. entersub(g): Void
  3. entersub(h): Scalar
  4. list: Scalar

And in void context:

( f(), g(), h() ); 1;
  1. entersub(f): Void
  2. entersub(g): Void
  3. entersub(h): Void
  4. list: Void

And now we're left with the fourth case:

sub { ( f(), g(), h() ) }
  1. entersub(f): From subroutine's caller
  2. entersub(g): From subroutine's caller
  3. entersub(h): From subroutine's caller
  4. list: From subroutine's caller

If we call the subroutine as

@x = sub { ( f(), g(), h() ) }->();

then all's fine:

  1. entersub(f): From subroutine's caller: List
  2. entersub(g): From subroutine's caller: List
  3. entersub(h): From subroutine's caller: List
  4. list: From subroutine's caller: List

But if we call the subroutine as

$x = sub { ( f(), g(), h() ) }->();

we have a bug!

  1. entersub(f): From subroutine's caller: Scalar: should be void!
  2. entersub(g): From subroutine's caller: Scalar: should be void!
  3. entersub(h): From subroutine's caller: Scalar
  4. list: From subroutine's caller: Scalar

Some code to support what I said:

use strict; use warnings; sub cx { print !defined(wantarray()) ? 'v' : !wantarray() ? 's' : 'l'; } my ($x, @x); print('v: '); ( cx(), cx(), cx() ) ; print("\n"); print('cv: '); sub { ( cx(), cx(), cx() ) }->(); print("\n\n"); print('s: '); $x = ( cx(), cx(), cx() ) ; print("\n"); print('cs: '); $x = sub { ( cx(), cx(), cx() ) }->(); print("\n\n"); print('l: '); @x = ( cx(), cx(), cx() ) ; print("\n"); print('cl: '); @x = sub { ( cx(), cx(), cx() ) }->(); print("\n");
v: vvv cv: vvv s: vvs \ mismatch cs: sss / l: lll cl: lll

A practical difference:

use strict; use warnings; my $x; $x = ( 'abc', 'def' ) ; # Warns $x = sub { ( 'abc', 'def' ) }->(); # Doesn't warn
Useless use of a constant in void context at line 5.

* — A list op in list context is subsequently optimized away since it's a no-op there. This does not affect the results.


In reply to Re: Context: compile-time vs. run-time by ikegami
in thread Context: compile-time vs. run-time by shmem

Title:
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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (5)
    As of 2015-07-30 11:25 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 (271 votes), past polls