To bring an offlist conversation to you, I'm a little confused. Yes, I
know you already know that, but this time I'm a little confused about
something specific. Something about Perl.

Being a bit of a dozy pillock, I expected that when I ran this:
----code----
#!/usr/bin/perl -w

use strict;

my %fred;

$fred{'barney'} = "wilma";


######## case 1 #########

print "Case 1:\n";

(exists $fred{'barney'}) ? print "the key exists\n" : print "the key does
not exist\n";

print "...\nCase 2:\n";


######## case 2 #########

my $foo = "";

(exists $fred{'barney'}) ? $foo .= "the key exists\n" : $foo .= "the key
does not exist\n";

print $foo;

print "...\nCase 3:\n";



######## case 3 #########

my $bar = (exists $fred{'barney'}) ? "the key exists\n" : "the key does
not exist\n";

print $bar;

print "...\nCase 4:\n";



######## case 4 #########

my $baz = "";
(exists $fred{'barney'}) ? $baz .= "the key exists\n" 
                                    && print "YO!\n"
                         : $baz .= "the key does not exist\n" 
                                    && print "NO!\n" ;
print "$baz\n";

exit 0;

----code----

to get this:

----expected output----
Case 1:
the key exists
...
Case 2:
the key exists
...
Case 3:
the key exists
...
Case 4:
YO!
the key exists
----expected output----

However, I actually get this:

----output----
Case 1:
the key exists
...
Case 2:
the key exists
the key does not exist
...
Case 3:
the key exists
...
Case 4:
YO!
NO!
11
----output----

I don't know why since I thought that only one expression would be
evaluated if you used the ()?:; operator. I'm sure it's more me not having
the morlocks to figure it out than unexpected behaviour on the part of
perl, so can one of you Very Clever People explain it to me please?

Pretty please? It'd make my weekend. Thank you.

-- 
matt


Reply via email to