conditional operator "?:" 
Author Message
 conditional operator "?:"

Hi everyone,

I'm trying to check a variable for validity. If positive, it will keep it's
value, if not, the variable will be emptied (or undefined, or assigned with
something else).

Here's what I have:

#################################
#!/usr/local/bin/perl -w
use strict;
my ($empty1, $empty2);

my $test = "it contains |";
my $empty = ( ($test =~ /\|+/) ? $test : "" );

my $test1 = "it contains |";
($test1 =~ /\|+/) ? $empty1 = $test : undef $empty1;

my $test2 = "it contains |";
($test2 =~ /\|+/) ? $empty2 = $test : $empty2 = "it doesn't contain |";

print "$empty, $empty1, $empty2\n";
#################################

It prints: "it contains |, it contains |, it doesn't contain |"

The first two tests did work ok.
I just don't understand why the third test doesn't work as expected.

I looked for "perldoc -q operators" and found out:

     Although it has the same precedence as in C, Perl's "?:"
     operator produces an lvalue.  This assigns $x to either $a
     or $b, depending on the trueness of $maybe:

         ($maybe ? $a : $b) = $x;

But it doesn't talk about the way I did it for the second and third test.

Christian



Sat, 06 Aug 2005 03:49:16 GMT  
 conditional operator "?:"

cc> my $test2 = "it contains |";
cc> ($test2 =~ /\|+/) ? $empty2 = $test : $empty2 = "it doesn't contain |";

cc> print "$empty, $empty1, $empty2\n";
cc> #################################

cc> It prints: "it contains |, it contains |, it doesn't contain |"

cc> The first two tests did work ok.
cc> I just don't understand why the third test doesn't work as expected.

Parentheses are always helpful...what you've got is being
evaluated as:

  ($test2 =~ /\|+/) ? ($empty2 = ($test : $empty2 = "it doesn't contain |"));

To be sure, add parens:

  ($test2 =~ /\|+/) ? ($empty2 = $test) : ($empty2 = "it doesn't contain |");

...cj

--
------------------------
-- Christopher J. White                                    
--

--   grierwhite.com
------------------------



Sat, 06 Aug 2005 05:58:59 GMT  
 conditional operator "?:"

Quote:

> my $test2 = "it contains |";
> ($test2 =~ /\|+/) ? $empty2 = $test : $empty2 = "it doesn't contain |";
> I just don't understand why the third test doesn't work as expected.

Precedence.

You can override it with parens to get what you want:

  ($test2 =~ /\|+/) ? $empty2 = $test2 : ($empty2 = "it doesn't contain |");

But I would count that use an abuse of the ?: operator, because
it contains side effects (assignment).

Quote:
> I looked for "perldoc -q operators" and found out:

   perldoc perlop

is where the precedence table is.

Note that ?: is higher precedence than assignment.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Sat, 06 Aug 2005 06:11:25 GMT  
 conditional operator "?:"

Quote:

>my $test2 = "it contains |";
>($test2 =~ /\|+/) ? $empty2 = $test : $empty2 = "it doesn't contain |";

I recommend using the '$var = (cond) ? "true" : "false";' form of the
conditional operator.  Your $empty2 line can be rewritten as:

  $empty2 = ($test2 =~ /\|+/) ? $test : "it doesn't contain |";

        -Joe

--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Sat, 13 Aug 2005 20:40:38 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. "open" range operator

2. Question about "->" operator

3. HowTo "break" and ?: operator

4. Possible bug in "or" operator

5. proposed "??" operator causing ambiguous syntax

6. perl 4.036 "or" operator

7. "character class ""bug""

8. "Best" way to do a conditional "use ..."

9. system(("cp", "-Rf", "/tmp/a/*", "/tmp/b")); doesnt wrk

10. how to parse a "this", "that", "and the ", "other" file

11. "->" operator and "unquoted string warning"

12. s"\n+$"" s"\n*$""

 

 
Powered by phpBB® Forum Software