nysus has asked for the wisdom of the Perl Monks concerning the following question:

I ran across this line in a Perl program I'm trying to deconstruct:

$line =~ s/[ \011]*\015$//; # Chop trailing whitespace and DOS CRs

I'm pretty familiar with regular expressions but this just makes no sense to me. I don't see how the pattern to be replaced translates to whitespaces and carriage returns. Are the '011' and '015' ascii codes? It looks like they are escaped, too. Still, this makes no sense to me, especially because these 011 is a vertical tab and 015 is a 'shift in'. Can someone please explain? Thanks.

Replies are listed 'Best First'.
Re: Quickie code question
by arturo (Vicar) on Mar 28, 2001 at 21:46 UTC

    yes, \011 and \015 are codes, but they're octal codes, so that's ascii 9 and 13 respectively.

    You might be more familiar with these as \t and \r; although I suppose the octal codes are more 'leet'.

    The meaning of \0 is documented in perldoc perlre, which is something with which anyone who *really* wants to know REs should be familiar =)</gentle poke>

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Quickie code question
by marius (Hermit) on Mar 28, 2001 at 21:41 UTC
    The escaped \011 and \015 are octal character codes. See perldoc perldata under Scalar Value Constructors for more information about these.

    Incidentally, to validate this, try:
    perl -e 'print 011 . "\n"'
    It won't print what you're expecting, I bet.

    -marius
Re: Quickie code question
by converter (Priest) on Mar 28, 2001 at 21:49 UTC

    The pattern would probably be easier to understand at a glance (and less dependent on the host's character set) if it were written:

    $line =~ s/[ \t]*\r$//;

    Make sense to you now?

      or even

      $line =~ s/\s*\r$//;


      "Only Bad Coders Badly Code In Perl" (OBC2IP)

        Right. But the author didn't say he wanted to remove formfeeds. <g>

Re: Quickie code question
by nysus (Parson) on Mar 29, 2001 at 02:19 UTC
    Thanks for all of your answers. I hope in a couple months I'll be proficient enough in Perl to give as much as I am getting. Thanks to eveyone who helped put this board together. This is a fantastic resource. Where can I send a few bucks?