Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Little annoying mistakes ... of others

by toolic (Chancellor)
on Dec 07, 2008 at 03:08 UTC ( #728636=note: print w/ replies, xml ) Need Help??


in reply to Little annoying mistakes ... of others

A pretty common logic error is forgetting to explicitly check all expected values against the variable:

use strict; use warnings; my $foo = 'boo'; if ($foo eq 'goo' || 'moo') { print "$foo\n"; }

when this is really desired:

if ($foo eq 'goo' || $foo eq 'moo') {

Obviously, this is not unique to Perl, but I do see it quite often.


Comment on Re: Little annoying mistakes ... of others
Select or Download Code
Re^2: Little annoying mistakes ... of others
by szabgab (Priest) on Dec 07, 2008 at 04:39 UTC
    This is exactly the type of examples I am looking for.

    Funny as I have just seen this happen in my last class 4 days ago.

Re^2: Little annoying mistakes ... of others
by parv (Priest) on Dec 07, 2008 at 11:44 UTC
    if ($foo eq 'goo' || 'moo') { ... }
    Error that may be but I do wish somethimes that that ought to be correct.
      goto Perl 6;
Re^2: Little annoying mistakes ... of others
by jeffa (Chancellor) on Dec 07, 2008 at 16:06 UTC

    Actually ... i would desire this:

    use strict; use warnings; my $arg = shift || 'boo'; my @valid = qw( goo moo foo voo doo poo ); print "$arg matches\n" if grep /$arg/, @valid;

    But this bring up an annoyance as well, because this mistake will compile and always return true (as long as the array being checked is not empty):

    # wrong way ahead! print "$arg matches\n" if grep $arg, @valid;
    Oops!

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Wow, I am writing down the examples as test cases but I can't figure out why is the second example always true?

      could you please explain?

        I think this:

        print "$arg matches\n" if grep $arg, @valid;

        Is equivalent to this:

        print "$arg matches\n" if grep { $arg } @valid;
        So as long as @valid and $arg are not false, then the match will also be true. (If $arg is 0 (zero) then the second example will not always be true.)

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://728636]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-12-28 17:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls