marcelo.magallon has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow Monks,

this one has me baffled...

I have the following code which reads files like http://oss.sgi.com/projects/ogl-sample/registry/APPLE/client_storage.txt

sub read_spec($) { my $filename = shift; local $/; open(my $fh, "<$filename") or die "Can't open filename"; my $content = <$fh>; $content =~ s{\s+$}{}m; # ... }

If you take a look at the file I mentioned above, you'll notice it has this:

Name

    APPLE_client_storage
<tab here>
Name Strings
...

Running the above code on this file leaves the tab character in place.

Adding:

$content =~ s{\s+$}{}m; $content =~ s{\t+$}{}m;

has the desired effect. As well as this:

$content =~ s{[ \t]+$}{}m;

Ideas? My understanding of \s is flawled somewhere. After following manpages around, is ended up in isspace(3), which makes me beleive that the code should work as I expect it to.

Marcelo

Update: Fixed formatting...

Replies are listed 'Best First'.
Re: \s vs \t
by ikegami (Patriarch) on Jan 05, 2005 at 17:15 UTC
    It doesn't match your tab cause it's already matched something earlier in the file. You want
    s{\s+$}{}mg
    rather than
    s{\s+$}{}m.

    Update: Removed incorrect statement about what was being matched. And added the following comment:

    \s can match newlines, which produces something undesireable.
    s{[\t ]+$}{}mg
    does not suffer the same problem.

      Out of curiousity, how would s/\s+$// match anything to the LEFT of something other than a newline or end-of-string?

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        silly me! I kept thinking he was doing /^\s+/. It was actually matching a newline. Updated.

      Oh.

      Thanks, I was mucking around with m and s and removed the g because it had a side effect I didn't want...

      Many thanks!