http://www.perlmonks.org?node_id=786056


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.

Replies are listed 'Best First'.
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"

      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.

      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