Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why do these "?:" conditionals work differently?

Reply
Thread Tools

Why do these "?:" conditionals work differently?

 
 
malgosia askanas
Guest
Posts: n/a
 
      09-24-2003
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
 
Reply With Quote
 
 
 
 
JS Bangs
Guest
Posts: n/a
 
      09-24-2003
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://www.velocityreviews.com/forums/(E-Mail 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?"
 
Reply With Quote
 
 
 
 
bd
Guest
Posts: n/a
 
      09-24-2003
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

 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      09-24-2003
malgosia askanas <(E-Mail Removed)> 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
(E-Mail Removed)
 
Reply With Quote
 
malgosia askanas
Guest
Posts: n/a
 
      09-24-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
C# to VB.net conversion for "?" conditionals .Net Sports ASP .Net 15 02-03-2006 04:11 PM
XSD: ways to deal with conditionals on attribute value? Piper707@hotmail.com XML 1 12-30-2005 09:10 PM
Conditionals & paths Mustafa Aydin XML 0 04-21-2004 05:25 PM



Advertisments