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":



  • 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, details, 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, summary, 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.