Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: AND OR

by davidrw (Prior)
on May 14, 2006 at 14:19 UTC ( [id://549320]=note: print w/replies, xml ) Need Help??


in reply to Re: AND OR
in thread AND OR

(for keyword or further-search purposes) that behavior is dubbed "short-circuiting"

And i'll toss in a good C example (avoid method calls on undef/null):
int *x; if(ptr != NULL && *ptr==5){ ... } if( objPtr != NULL && objPtr->foo() ){ ... } # extended to perl: my $obj = Foo::Bar->new(); if( $obj && $obj->blah() ){ ... }
Update: added parens for ->foo()

Replies are listed 'Best First'.
Re^3: AND OR (C meth)
by tye (Sage) on May 14, 2006 at 16:29 UTC

    Heh, you hit a C pet peave of mine. This:

    if( objPtr != NULL && objPtr->fo­o ){ ... }

    doesn't actually call any methods. You need objPtr->foo() for that. And the ANSI C committee should have made the "&" for getting a pointer to a method mandatory instead of optional and discouraged, as needing to do that is relatively rare, rarer than the mistake of not realizing that C requires the parens unlike several other quite similar languages. Further, this particular mistake usually results in compiler output that is quite difficult to diagnose, IME.

    - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found