Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: About a piece of code

by olus (Curate)
on Aug 05, 2009 at 11:14 UTC ( #786056=note: print w/ replies, xml ) Need Help??


in reply to Re^2: About a piece of code
in thread About a piece of code

Take a look at Perl regular expressions. There you'll find the information you need to get you started on regexps.

So, you have a regexp in that piece of code, and the and next; part is saying that, if the line that was read matches that condition, then you don't want to execute the rest of the code inside the while statement, you want to read a new line from input, meaning that the next code to be executed is while (<input>). That is to say, you go for another cycle of the while loop.


Comment on Re^3: About a piece of code
Select or Download Code
Re^4: About a piece of code
by trewq (Initiate) on Aug 05, 2009 at 11:42 UTC
    olus you say: and next; part is saying that, if the line that was read matches that condition, then you don't want to execute the rest of the code inside the while statement, you want to read a new line from input, meaning that the next code to be executed is while (<input>).
    while (<input>) { m/^\n/ and next; m/^\n/ and next; ... }
    if the code was like above, wouldnt the second "m/^\n/ and next;" line be executed?

    Because you said "if the line that was read matches that condition, then you don't want to execute the rest of the code inside the while statement"

      Imagine you're The Soup Nazi, a patron steps forward in line (readline) with a giant \n on their forehead, and you yell NEXT! and kick them out. Then the next person steps forward with a giant "Foo\n" on their forehead. Maybe you give them soup, maybe you make them pay for bread, maybe they object and you yell "No soup for you!" and kick them out :)

      nosoup.pl

      #!/usr/bin/perl -- while(<DATA>){ /^\s+$/ and next; if(/George/){ print "No soup for you!\n"; } print "What soup you want? $_"; } __DATA__ Kramer Elaine George
      and Devel::Trace output
      $ perl -d:Trace nosoup.pl >> nosoup.pl:2: while(<DATA>){ >> nosoup.pl:3: /^\s+$/ and next; >> nosoup.pl:3: /^\s+$/ and next; >> nosoup.pl:4: if(/George/){ >> nosoup.pl:7: print "What soup you want? $_"; What soup you want? Kramer >> nosoup.pl:3: /^\s+$/ and next; >> nosoup.pl:3: /^\s+$/ and next; >> nosoup.pl:4: if(/George/){ >> nosoup.pl:7: print "What soup you want? $_"; What soup you want? Elaine >> nosoup.pl:3: /^\s+$/ and next; >> nosoup.pl:3: /^\s+$/ and next; >> nosoup.pl:4: if(/George/){ >> nosoup.pl:5: print "No soup for you!\n"; No soup for you! >> nosoup.pl:7: print "What soup you want? $_"; What soup you want? George
      No, the second regex would NOT be executed.

      The second is inside your while; execution never gets there on a line with nothing but a return. Rather, the first "and next" sends execution back to the next while test.

      This is the kind of thing you can test for yourself. As another user was advised, in paraphrase, the universe won't end when you try out your (safe) code (caveat: don't try this with code that could muck about with your system... or anybody else's. cf rm, unlink, etc.

      In this case, a simple way to explore your question might be:

      #!/usr/bin/perl use strict; use warnings; #786068 while (<DATA>) { m/^\n/ and next; m/^\n/ and next; print; } __DATA__ line 1 line2 line4, preceeded by blank line ln 5 ln6 ln8 preceeded by 2 blank lines ln9

      Go ahead; try it! And, for extra credit, write a script that makes the output clear(er) to you (hint: put the second regex in a conditional and do something if a match occurs). Alt: run the script under the debugger, watching line 9 of the code above.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-11-01 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (228 votes), past polls