Why do these "?:" conditionals work differently?

Discussion in 'Perl Misc' started by malgosia askanas, Sep 24, 2003.

  1. In perl5.6.1, this code:

    $DEBUG = 1;
    $DEBUG ? $prefix = 'foo' : $prefix = 'bar';
    print $prefix

    prints "foo", even though $DEBUG is set to 1. On the other hand, this code:

    $DEBUG = 1;
    $DEBUG ? print "foo\n" : print "bar\n";

    prints "foo", as I would expect it to (and prints "bar" if $DEBUG is set to 0).
    Why doesn't the first piece of code set $prefix to "bar"?


    Many thanks in advance,
    -malgosia
     
    malgosia askanas, Sep 24, 2003
    #1
    1. Advertising

  2. malgosia askanas

    JS Bangs Guest

    malgosia askanas sikyal:

    > In perl5.6.1, this code:
    >
    > $DEBUG = 1;
    > $DEBUG ? $prefix = 'foo' : $prefix = 'bar';
    > print $prefix
    >
    > prints "foo", even though $DEBUG is set to 1. On the other hand, this code:


    Eh? This is exactly what it should do. $DEBUG is true, so the code between
    ? and : is executed, making $prefix equal 'foo'. If $DEBUG is *false*,
    then we expect $prefix to be 'bar'.

    > $DEBUG = 1;
    > $DEBUG ? print "foo\n" : print "bar\n";
    >
    > prints "foo", as I would expect it to (and prints "bar" if $DEBUG is set to 0).
    > Why doesn't the first piece of code set $prefix to "bar"?


    These two pieces of code work exactly the same. The first piece of code
    doesn't set $prefix to "bar" because $DEBUG is true.

    --
    Jesse S. Bangs
    http://students.washington.edu/jaspax/
    http://students.washington.edu/jaspax/blog

    Jesus asked them, "Who do you say that I am?"

    And they answered, "You are the eschatological manifestation of the ground
    of our being, the kerygma in which we find the ultimate meaning of our
    interpersonal relationship."

    And Jesus said, "What?"
     
    JS Bangs, Sep 24, 2003
    #2
    1. Advertising

  3. malgosia askanas

    bd Guest

    malgosia askanas wrote:

    > In perl5.6.1, this code:
    >
    > $DEBUG = 1;
    > $DEBUG ? $prefix = 'foo' : $prefix = 'bar';
    > print $prefix
    >
    > prints "foo", even though $DEBUG is set to 1. On the other hand, this
    > code:
    >
    > $DEBUG = 1;
    > $DEBUG ? print "foo\n" : print "bar\n";
    >
    > prints "foo", as I would expect it to (and prints "bar" if $DEBUG is set
    > to 0). Why doesn't the first piece of code set $prefix to "bar"?


    Why wouldn't it? You didn't change the order, so it should work the same
    way.
    --
    Hard work never killed anybody, but why take a chance?
    -- Charlie McCarthy
     
    bd, Sep 24, 2003
    #3
  4. malgosia askanas <> wrote:
    > In perl5.6.1, this code:
    >
    > $DEBUG = 1;
    > $DEBUG ? $prefix = 'foo' : $prefix = 'bar';
    > print $prefix
    >
    > prints "foo", even though $DEBUG is set to 1. On the other hand, this code:
    >
    > $DEBUG = 1;
    > $DEBUG ? print "foo\n" : print "bar\n";
    >
    > prints "foo", as I would expect it to (and prints "bar" if $DEBUG is set to 0).
    > Why doesn't the first piece of code set $prefix to "bar"?


    The ?: operator has higher precedence than =, so I think your first
    example is equivalent to
    ($DEBUG ? $prefix = 'foo' : $prefix) = 'bar';
    Which evaluates to
    $prefix = 'foo' = 'bar';

    You probably want:
    $prefix = $DEBUG ? 'foo' : 'bar';

    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Sep 24, 2003
    #4
  5. I wrote:

    >> In perl5.6.1, this code:
    >>
    >> $DEBUG = 1;
    >> $DEBUG ? $prefix = 'foo' : $prefix = 'bar';
    >> print $prefix
    >>
    >> prints "foo", even though $DEBUG is set to 1.


    I meant, of course, that it prints "bar", even though $DEBUG is set to 1.
    Many thanks to Glenn Jackman for seeing beyond my error and for explaining the
    mechanism:

    >The ?: operator has higher precedence than =, so I think your first
    >example is equivalent to
    > ($DEBUG ? $prefix = 'foo' : $prefix) = 'bar';
    >Which evaluates to
    > $prefix = 'foo' = 'bar';
    >
    >You probably want:
    > $prefix = $DEBUG ? 'foo' : 'bar';
    >


    -malgosia
     
    malgosia askanas, Sep 24, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ß Ø ® G
    Replies:
    8
    Views:
    358
    ß Ø ® G
    Oct 6, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    907
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,984
    Smokey Grindel
    Dec 2, 2006
  4. JohnJSal
    Replies:
    13
    Views:
    440
    John Salerno
    Nov 4, 2006
  5. M. Hamed

    Why these don't work??

    M. Hamed, Apr 8, 2010, in forum: Python
    Replies:
    9
    Views:
    233
    Lie Ryan
    Apr 9, 2010
Loading...

Share This Page