CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [ashish_chand] condtion check: Edit Log



Kenosis
User

Mar 2, 2013, 12:36 PM


Views: 797
Re: [ashish_chand] condtion check

What a great question! There's some Perl short circuiting demonstrated here. For example:

Code
$f == 1 && $f = 0;

is functionally equivalent to:

Code
$f = 0 if $f == 1;

Why? Perl evaluates the first conjunct $f == 1 and, if true, proceeds to evaluate the second so the overall && statement can be evaluated. However, if the first conjunct is false, there's no need to evaluate the second, since the overall && statement is (already) false. This is the short circuit effect and why it's equivalent to $f = 0 if $f == 1. Obviously, this can impact code readability, as does using variables named only $f and $q.

We can see exactly how Perl views this short circuiting:

Code
perl -MO=Deparse,-P -e '$f == 1 and $f = 0' 
$f = 0 if $f == 1;
-e syntax OK


Notice that it parsed the and statement (used here instead of &&) as a conditional statement.

Curiously, the code's author implicitly used Perl's default scalar, viz., $_, in the matching statement /"/, yet chose to use substr to remove its first character, instead of a substitution like s/.//.


(This post was edited by Kenosis on Mar 2, 2013, 12:38 PM)


Edit Log:
Post edited by Kenosis (User) on Mar 2, 2013, 12:38 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives