Why do these "?:" conditionals work differently?

M

malgosia askanas

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
 
J

JS Bangs

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 (e-mail address removed)
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?"
 
B

bd

malgosia said:
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.
 
G

Glenn Jackman

malgosia askanas said:
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';
 
M

malgosia askanas

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top