Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

About a piece of code

by trewq (Initiate)
on Aug 05, 2009 at 10:38 UTC ( #786040=perlquestion: print w/ replies, xml ) Need Help??
trewq has asked for the wisdom of the Perl Monks concerning the following question:

Hello, while (<input>) { m/^\n/ and next; ... } In the above code 1) What does m/^\n/ mean? 2) What is "and next;" for? Its meaning? Thanks.

Comment on About a piece of code
Re: About a piece of code
by olus (Curate) on Aug 05, 2009 at 10:47 UTC

    m/^\n/ is a regular expression that tries to match a newline (\n) at the beggining of the line (^). If there is such a match on the current value of $_, the code jumps back to the condition on the while loop, in this case reads one more line from the file.

    So, this code is skipping blank lines on the file.

    update as Marshall correctly points, the code skips lines that start with the \n disregarding whatever comes next.
      This is skipping lines which only have a new line char at the start. m/^\s*$/ is a better way of saying a line with only white space characters because it allows any number of non-printing whitespace characters on the line. Whitespace is \n\f\s\f\r Update: oof..goof should be \n\f\s\t\r order doesn't matter, but there are 5 of these white space critters and I goofed! Ooops!

      m/^\n/ and next; ... }
      This is sort of a "cheater" way to execute the "next" if the match is true. Sometimes this is a good idea, sometimes not. I would probably code it like this:

      next if m/^\s*$/; #skip/re-prompt for blank lines #or if (m/^\s*$/) #very unlikely that I would write this { #but, it means the same as above next; } I would not write the below. This is obfuscated bad code. Because it obscures the "if" nature of the statement. m/^\s*$/ and next;
        Whitespace is \n\f\s\f\r Update: oof..goof should be \n\f\s\t\r order doesn't matter,

        \s is not a whitespace character.   In a regular expression it is a character class that includes the characters [ \t\n\r\f] and in a double quoted string it is the character s.

      olus Could you pls expand your explanation? Sorry, I am a beginner. and finally what exactly does "and next" do? Thanks.

        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.

Re: About a piece of code
by tweetiepooh (Friar) on Aug 05, 2009 at 11:02 UTC
    Simply put is skips blank lines.

    m/^\n/ is a regular expression for start of line followed by new line.

    and next means go the the next item in input if regular expression is true.

Re: About a piece of code
by rovf (Priest) on Aug 05, 2009 at 11:03 UTC

    perldoc is your friend.

    For m, I suggest that you have a look at perlop, i.e. do a

    perldoc perlop
    from your command line, and search for the chapter titled Regexp Quote-Like Operators. For and, you search for the chapter "Logical And", also in perlop. next is described in perlsyn.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: About a piece of code
by biohisham (Priest) on Aug 05, 2009 at 15:20 UTC
    Everybody has done great at telling you this much about this small query, I would just reiterate it in a different way by giving you the technical names and some aspects.

    "^" is considered to be an assertion, it gives you more control, so here the "^" makes you match the beginning of the line, without it there, the program logic would go back and do another loop journey every time it encounters "\n" but you want to only capture those new-line characters that happen at the beginning , you got other assertions to match the end of the line or string or word boundary...etc, read about them,because regular expression is so important in Perl,without it you are in a fix

    "\" in \n is an escape character, it helps to extend a meaning to an otherwise normal letter, like t and \t are not the same thing

    the most important feature here in your question is the "and", this is sort of an if statement but in a different way, Marshall has given you useful tips on other ways to achieve the same thing, here I would just add to you that this special "and" is called "logical and" operator, it works as a short-circuit, that is, unless the left expression is true it would not proceed to evaluate the right expression or operand since operators work on operands mostly right?

    so what happened here is that if the left expression is true, (a string beginning with a new line) the "and" would evaluate the right expression, next, next means repeat over the loop from the start again.
    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2014-07-30 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls