http://qs321.pair.com?node_id=786041


in reply to About a piece of code

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.

Replies are listed 'Best First'.
Re^2: About a piece of code
by Marshall (Canon) on Aug 05, 2009 at 10:53 UTC
    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.

        \s is the single space ' ' character. In your set [ \t\n\r\f] that thing right before the \t is \s.

        Oooh I see now... in a character set [\s\t\f\r\n], \s means a single space.
        in a regex \s means all of the chars in this set: [\s\t\f\r\n]. Yep, confusing!!
        \s has a context dependent meaning. Such as it is.

Re^2: About a piece of code
by trewq (Initiate) on Aug 05, 2009 at 10:55 UTC
    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.

        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"