Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
So, what value do you see in advocating this model?

I prefer to stick to models that, well, model what actually happens. And what actually happens is that the , operator really does have a scalar-context mode that gives the left operand void context and the right operand scalar context.

However, in the case of a literal list returned by a sub, there are complications. Context in perl is somewhat limited. It can either be derived at compile time or looked up at runtime. If the latter, it is the context in which the current sub, eval, or mainline code is called (which itself may or may not be known at compile time).

But this isn't complex enough to fully handle a literal list. When a literal list appears in list context, each component should have list context. This works fine. When a literal list appears in void context, each component should have void context. This also works fine. But when a literal list appears in (not known at compile time) scalar context, every component but the last should have void context. And perl's context propagation isn't smart enough for that. Instead every component gets scalar context and a cleanup operator (named "list") ensures that in fact the list of results is reduced to the last scalar. This is quite clearly a case of a list in scalar context.

The following code demostrates this, using a tied array as a left operand to a comma operator in each of void, scalar, and list context, first where the context is known at compile time, and then where it isn't:

$ perl $ perl use Tie::Array; @ISA="Tie::StdArray"; sub FETCHSIZE{print "in FETCHSIZE\n";my $self=shift;$self->SUPER::FETC +HSIZE(@_)}; tie @x,"main"; @x=1..3; print "void\n"; @x,1; print "scalar\n"; $x=(@x,1); print "list\n"; ()=(@x,1); print "void\n"; sub{@x,1}->(); print "scalar\n"; $x=sub{@x,1}->(); print "list\n"; ()=sub{@x,1}->(); __END__ void scalar list in FETCHSIZE void scalar in FETCHSIZE list in FETCHSIZE
Note that where the scalar context is known at compile time, the left operand to , got void context, but where it was not known, it got scalar context.

In reply to Re: If you believe in Lists in Scalar Context, Clap your Hands by ysth
in thread If you believe in Lists in Scalar Context, Clap your Hands by oshalla

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
  • Outside of code tags, you may need to use entities for some characters:
            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 making s'mores by the fire in the courtyard of the Monastery: (6)
    As of 2014-04-21 11:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (493 votes), past polls