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:
condtion check

 



ashish_chand
Novice

Mar 1, 2013, 4:13 AM

Post #1 of 8 (849 views)
condtion check Can't Post

I am new to the scripting and have few line of code to analyse:

if ( $f == 0 ) { $f=1; /"/ && do { $q=0; $_=substr($s,0,1,"") } };
if ( $q == 0 && /"/ ) { $_=substr($s,0,1,"");
if (/^$/) {$q=1} elsif (/,/) {$f=0;$q=1} };
if ( $q == 1 && /,/ ) { $f=0 };



in the above line i can see the condition is being checked for /"/ and /,/

can anyone please explain how its working and also explain the above line.


BillKSmith
Veteran

Mar 1, 2013, 6:27 AM

Post #2 of 8 (843 views)
Re: [ashish_chand] condtion check [In reply to] Can't Post

The following is a less idiomatic, but equivalent perl code.

Code
my $DOUBLE_QUOTE = q("); 
my $NULL_STRING = q();
my $COMMA = q(,);

if ( $f == 0 ) {
$f = 1;
if ($_ =~ /$DOUBLE_QUOTE/) { # $_ contains a DOUBLE QUOTE character?
$q = 0;
$_ = substr($s, 0, 1, $NULL_STRING); # Remove first character of $s.
}
}


if ( $q == 0 ) {
if ( $_ =~ /$DOUBLE_QUOTE/ ) { # $_ contains a DOUBLE QUOTE character?
$_ = substr( $s, 0, 1, $NULL_STRING); # Remove first character of $s.
}
}

if ( $_ =~ /^$/ ) { # $_ is a NULL_STRING?
$q = 1;
}
elsif ( $_ =~ /$COMMA/ ) { # $_ contains a COMMA?
$f = 0;
$q = 1;
}

if ( $q == 1 ) {
if ( $_ =~ /$COMMA/ ) {
$f = 0;
}
}

Good Luck,
Bill


Kenosis
User

Mar 2, 2013, 12:36 PM

Post #3 of 8 (828 views)
Re: [ashish_chand] condtion check [In reply to] Can't Post

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)


Laurent_R
Veteran / Moderator

Mar 2, 2013, 2:34 PM

Post #4 of 8 (821 views)
Re: [Kenosis] condtion check [In reply to] Can't Post


In Reply To
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/.//.


Yes, I also found it a bit unusual to find the use of this plain vanilla use of the substr function in a piece of code that is otherwise highly Perl idiomatic and in which I would also have expected rather an s/// substitution.

But I guess I probably just do the same thing from time to time, mixing very perlish constructs with plain vanilla code.


Kenosis
User

Mar 2, 2013, 3:19 PM

Post #5 of 8 (820 views)
Re: [Laurent_R] condtion check [In reply to] Can't Post


Quote
But I guess I probably just do the same thing from time to time, mixing very perlish constructs with plain vanilla code.


Well, I've certainly never been guilty of doing that!!! Smile


Laurent_R
Veteran / Moderator

Mar 2, 2013, 3:55 PM

Post #6 of 8 (818 views)
Re: [Kenosis] condtion check [In reply to] Can't Post

Hmmm, I did not mean this to be a guilt.

Wink

Or, probably, you are making fun about me. Frown Oh, well, go on, no problem, I probably did not explain my point properly.


(This post was edited by Laurent_R on Mar 2, 2013, 4:01 PM)


Kenosis
User

Mar 2, 2013, 4:07 PM

Post #7 of 8 (815 views)
Re: [Laurent_R] condtion check [In reply to] Can't Post

No, no, Laurent_R! I was only trying to say that I've done exactly the same thing! Sorry that my words weren't well chosen (or that my attempt at sarcasm failed)... Smile


(This post was edited by Kenosis on Mar 2, 2013, 4:27 PM)


ashish_chand
Novice

Mar 3, 2013, 10:48 PM

Post #8 of 8 (798 views)
Re: [BillKSmith] condtion check [In reply to] Can't Post

hi Smith ,

Thanks for providing me the soluion. it really make clear to get the logic used in the code.

 
 


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

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