Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Using ternary operator as lvalue in push

by ysth (Canon)
on Jul 31, 2007 at 19:03 UTC ( [id://629895]=note: print w/replies, xml ) Need Help??


in reply to Using ternary operator as lvalue in push

Did you happen to see what your friend was testing? Or what version he was using? There's been a bug for a long time that something like push CONSTANT-EXPRESSION ? @a : @b, LIST; works when it shouldn't, where CONSTANT-EXPRESSION is a constant or operations that result in a constant, like:
use constant OS => $^O; push OS eq "linux" ? @a : @b, "tux";
Don't rely on this to continue working: it's clearly a bug. Always use the @{ expression resulting in an arrayref } instead.

Using non-constant expressions before the ?, it fails for me on 5.6.2, 5.8.0, 5.8.8, and 5.9.5.

Replies are listed 'Best First'.
Re^2: Using ternary operator as lvalue in push
by dada (Chaplain) on Aug 01, 2007 at 09:54 UTC
    ouch... I'm oha's friend :-)

    of course, I tested it with:
    # This is perl, v5.8.7 my @a = (1,2,3); my @b = (4,5,6); push 1==1 ? @a : @b, "foo"; print "a=@a b=@b\n";
    I even counter-checked (!) it with:
    push 1==0 ? @a : @b, "foo";
    to verify that the "foo" was pushed into @b.

    of course, such stupid conditionals are optimized away. sorry for fooling myself, oha, and probably others. shame on me :-)

    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris
      Compile time constant removal magic.

      perl -MO=Deparse play produces:

      my(@a) = (1, 2, 3); my(@b) = (4, 5, 6); push @a, 'foo'; print "a=@a b=@b\n";

      /msg self Read ALL the post before replying

Re^2: Using ternary operator as lvalue in push
by oha (Friar) on Aug 01, 2007 at 09:17 UTC
    could be, i will ask him. i used a code that could be optimized by compiler:
    my $c = 1; push $c>0 ? @a : @b, "foo";
    maybe different settings would allow the compiler to reduce the condition at compile-time (my friend case) resulting in
    push @a, "foo";
    that will explain those behaviour

    Oha

      my $c = 1; push $c>0 ? @a : @b, "foo";
      is not optimized by the compiler. It only folds constants. $c is not a constant. On the other hand, the following would be optimized:
      use constant c => 1; push c>0 ? @a : @b, "foo";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://629895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-28 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found