[Omaha.pm] not defined $bucket || $bucket eq '???'

Theodore Katseres tedkat at gmail.com
Thu Dec 4 07:24:26 PST 2008


On Wed, Dec 3, 2008 at 6:50 PM, Jay Hannah <jay at jays.net> wrote:

> From: brandonglesmann at gmail.com
> Date: December 3, 2008 4:42:30 PM CST
>
>> Disclaimer: I have been out of perl programming for over two years. So, I
>> am really rusty. Also I never used the perl debugger so I could be
>> misinterpreting the output. Lastly, I am a coward which is why I did not
>> reply all.
>>
>
> Well, thanks for saying it was OK for me to drag you into the sunshine so
> all can benefit from my stupidity.  :)
>
>  WOW! Ok, I read that 10 times and have asked other perl programmers in the
>> area to read it as well.
>>
>
> Other Perl programmers? Sweet! Are they on the Omaha Perl Mongers mailing
> list? Recruit them!  :)
>
>  couple questions:
>> 1. How are the cases different? If ' ' is true and then both cases of your
>> example should be the same.......right?!?! Obviously not since you changed
>> the code but I don't know why.
>>
>
> ' ' is very different from ''.   :)
>
> I'm often a hunt and peck coder, so whatever perl does is the right answer.
> I don't know that there necessarily is a "why". (Other than "because that's
> what perl's C source code says" -grin-). So let's try some things...
>
> First, note that '' is false and ' ' is true.
>
> $ cat j.pl
> print ''  ? 'yes ' : 'no ';
> print ' ' ? 'yes ' : 'no ';
> $ perl j.pl
> no yes
>
> But my thing was more like this:
>
> $ cat j.pl
> print ((not defined $j || 1) ? 'yes ' : 'no ');
> print ((not defined $j or 1) ? 'yes ' : 'no ');
> $ perl j.pl
> no yes
>
> -think,think,think-
>
> I'm betting this happens due to operator precedence. Reading "perldoc
> perlop" doesn't help me much at a glance since I'm not sure where 'defined'
> falls in the documented precedence order stack. But let's assume for a
> second that the precedence of 'defined' is higher than that of 'or' but
> lower than that of '||'. If that's true, then:
>
>   not defined $j || 1
>
> would be processed as:
>
>   $j || 1          true
>   defined true     true
>   not true         false
>
> So if 'defined' is a "nonassoc list operators (rightward)" then this is
> probably the correct answer and I feel all smart and stuff.   :)
>
>  2. How do you get a eq to return ' ' ?
>>
>
> You don't? Apparently it returns '' if false, 1 if true:
>
> $ perl -d -e 1
>  DB<1> x $j eq 'blah'
> 0  ''
>  DB<2> x $j eq ''
> 0  1
>
> perldoc perlop says:
>
>  Binary "eq" returns true if the left argument is stringwise equal to
>  the right argument.
>
> And '' is false, and '1' is true, so there you have it...  :)
>
> Did that help at all?
>

Just a thought...

C<||> is higher then C<not>  but
C<!> is higher then C<||> :P

If you put parentheses in the right place you get what you want.

cat t.pl
print (((not defined $j) || 1) ? 'yes ' : 'no ');
print (((not defined $j) or 1) ? 'yes ' : 'no ');
print ((! defined $j || 1) ? 'yes ' : 'no ');
print ((not defined $j or 1) ? 'yes ' : 'no ')
yes yes yes yes


-- 
Ted Katseres
     ||=O=||
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/omaha-pm/attachments/20081204/16348f85/attachment-0001.html>


More information about the Omaha-pm mailing list