Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re^2: About a piece of code

by Marshall (Monsignor)
on Aug 05, 2009 at 10:53 UTC ( #786046=note: print w/replies, xml ) Need Help??

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

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;

Replies are listed 'Best First'.
Re^3: About a piece of code
by jwkrahn (Monsignor) on Aug 05, 2009 at 14:27 UTC
    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.

        \s is not a single character in a regular expression, it is the character class [ \t\n\r\f].   See perlre.

        $ perl -le' use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper grep /\s/, map chr, 0 .. 255; ' $VAR1 = "\t"; $VAR2 = "\n"; $VAR3 = "\f"; $VAR4 = "\r"; $VAR5 = " ";

        In a double quoted string it is just the character "s".

        $ perl -le' use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper "\s"; ' $VAR1 = "s";
        I think that, since the common regex wisdom involves replacing a literal space in an /x-modified regex with \s, it's easy to think of \s as a replacement for a literal space—but, if you actually want a literal space (rather than just a single whitespace character) in such a regex, but outside a character class, then what you want is '\ ' (i.e., an escaped space).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2016-10-24 22:39 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (310 votes). Check out past polls.