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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


Mar 2, 2013, 12:36 PM

Views: 1428
Re: [ashish_chand] condtion check

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

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

is functionally equivalent to:

$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:

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