in reply to (Golf) Warningless Comparison

I think that may all be more trouble than it is worth. One of the big reasons that the warnings pragma came to be was so that warnings didn't have to be an all or nothing proposition.

#!/usr/bin/perl use strict; use warnings; my ($foo, $bar); print '1: ', $foo eq $bar, "\n"; # Uninitialized warning. { no warnings qw( uninitialized ); print '2: ', $foo eq $bar, "\n"; # No warnings emitted. } print '3: ', $foo eq $bar, "\n"; # Warns again.

Update: If you insist on taking the long way though, here's something that might^probably won't help.

Update: Following is the original text of my message. My code is incorrect. It contains a bug whereby it both emits a warning and gives an incorrect result if $x is set to 0 and $y is left undefined. It will not give this result if $x is undefined and $y is 0, however. Ouch. Ewwww. Ugly! Please accept my humble apologies for not thorougly testing.

I believe that:

((defined($x) && defined($y) && ($x eq $y)) || (!defined($x) && !defined($y))
is equivalent to the shorter and more readable:
( defined($x && $y) and ($x eq $y) ) or not defined($x || $y)
And if you were really playing golf you might write that as:
defined($x&&$y)&&$x eq$y||!defined($x||$y)

Edit and Update: Added readmore tags and show a nasty bug with my definedness shortcuts.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re^2: (Golf) Warningless Comparison
by tadman (Prior) on Sep 28, 2002 at 01:10 UTC
    I suppose that's a fair thing to assert. I'm not a huge fan of turning off alarms, but when it is the most efficient method, it's hard to argue otherwise.

    Thanks for the pointer on 'uninitialized'. That's what I need.
Re: Re: (Golf) Warningless Comparison
by demerphq (Chancellor) on Sep 29, 2002 at 18:58 UTC
    Your solution doesnt handle existance mismatches correctly.

    --- demerphq
    my friends call me, usually because I'm late....

      Other than the no warnings qw( uninitialized ); suggestion, my solution wasn't a complete solution and didn't pretend to be. That's why I prefaced it with the phrase, "here's something that might help."

      -sauoq
      "My two cents aren't worth a dime.";