Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Deparse shows that the parentheses in the OP are unnecessary, which is as expected, since the conditional operator ?: has a higher precedence than assignment (see Operator Precedence and Associativity). Consider:

#! perl use strict; use warnings; my @x = qw(Initial list of words 1); my $y = 'Initial string 1'; show('With parentheses'); my @z = ( time() ? @x = qw(Words_z from conditional) : $y ) = foo(); print "\@z = (@z)\n"; show('After assignment to LHS'); @x = qw(Initial list of words 2); $y = 'Initial string 2'; show('Without parentheses'); my @w = time() ? @x = qw(Words_w from conditional) : $y = foo(); print "\@w = (@w)\n"; show('After assignment to LHS'); sub foo { if (wantarray()) { print "\nfoo called in list context\n"; return qw(sub_foo word list); } else { print "\nfoo called in scalar context\n"; return 'String_from_foo'; } } sub show { my ($heading) = @_; print "\n$heading:\n\n"; print "\@x = (@x)\n"; print "\$y = '$y'\n"; }

Output:

17:35 >perl 678_SoPW.pl With parentheses: @x = (Initial list of words 1) $y = 'Initial string 1' foo called in scalar context @z = (String_from_foo) After assignment to LHS: @x = (Words_z from conditional) $y = 'Initial string 1' Without parentheses: @x = (Initial list of words 2) $y = 'Initial string 2' foo called in scalar context @w = (String_from_foo) After assignment to LHS: @x = (Words_w from conditional) $y = 'Initial string 2' 17:35 >perl -v This is perl 5, version 18, subversion 0 (v5.18.0) built for MSWin32-x +86-multi-thread-64int

It’s clear that foo is being evaluated in scalar context, even though the condition evaluates to true and so chooses the LHS. Since foo would still be evaluated in scalar context if the condition were false and the RHS chosen, it’s now clear why the compile error has disappeared: it is known at compile time that foo will be evaluated in scalar context.

But that leaves us with some unanswered questions:

  1. What exactly is the l-value returned by the conditional expression?
  2. Why does this l-value — which results from the assignment of a list to an array — put foo into scalar context?
  3. Why does the assignment to @x seemingly occur after the call to foo?

I have the feeling that there’s an obvious answer to all this, but I can’t see it. Which is, just possibly, related to the fact that in my neck of the woods the Ashes coverage finishes around 3:30 am... |-O

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^2: ternary operator as lvalue failing to induce evaluation of right-hand expression in list context by Athanasius
in thread ternary operator as lvalue failing to induce evaluation of right-hand expression in list context by ed_hoch

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found