Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Little annoying mistakes ... of others

by toolic (Bishop)
on Dec 07, 2008 at 03:08 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: Little annoying mistakes ... of others
by jeffa (Bishop) 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)
        
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 (Parson) 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;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-26 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found