After reading Conditional Operator, and delighting in the potential for unreadable code coming from using a conditional operator as an lvalue, I wondered how far in advance the lvalues have to be declared.

Huh? Well, we know that

my ( $c, $d ); 1 ? $c : $d = 'Hi';
is the same (to B::Deparse) as
my ( $c, $d ); $c = 'Hi';
but what if you don't want to declare the variables in advance? Well,
my $d; 1 ? my $c : $d = 'Hi';
is the same as
my $d; my $c = 'Hi';
In the other direction, here's a strict-safe program that B::Deparse makes non-strict-safe:
$ perl -MO=Deparse -e 'my $c; 1 ? $c : my $d; $d' my $c; $c; $d; -e syntax OK
*. How about if we declare both variables in the conditional?
$ perl -e '1 ? my $c : my $d' Invalid separator character '$' in attribute list at -e line 1, near " +$c : my " Execution of -e aborted due to compilation errors.
Now, maybe there's some reason that parsing : my $d as an attribute list is good, or at least expected; but I can't see why 1 ? my $c : my $d would be parsed that way, but 1 ? my $c : $d wouldn't. Would somemonk enlighten me?

UPDATE: * Notice that B::Deparse changes the semantics here (but I guess there's always the disclaimer that that might happen). For example,

our $d = 'Out'; { 1 ? 0 : my $d; $d = 'In'; } say $d;
prints Out, but is converted by B::Deparse into
our $d = 'Out'; { '???'; $d = 'In'; } say $d;
which prints In.

UPDATE: Oh, maybe it's that a colon followed by whitespace and an alphanumeric is always interpreted as the beginning of an attribute list, even if we're waiting for the interstitial colon of a conditional operator. Is this correct? If so, is it the desired behaviour? If so, should the precedence table in perlop be changed to reflect it?


In reply to Unexpected parsing by JadeNB

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.