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.
| [reply] [d/l] [select] |
|
|
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;
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
olus
Could you pls expand your explanation?
Sorry, I am a beginner.
and finally what exactly does "and next" do?
Thanks.
| [reply] |
|
|
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.
| [reply] [d/l] [select] |
|
|
|
|
|
|
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>
| [reply] [d/l] [select] |
Re: About a piece of code
by tweetiepooh (Hermit) 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. | [reply] [d/l] [select] |
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
| [reply] [d/l] |