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

Hi folks,

I've recently started to follow some "best practices" recommended to me, and while converting s/// to s{}{}xms I came upon a strangeness:

perlport suggests using
s/$CR?$LF/\n/;
to clean up input lines from strange operating systems, so I tried the following:
my $CR="\015"; my $LF="\012"; my $foo="unknown_os\n"; $foo =~ s{$CR?$LF}{\n}xms; print $foo;
This gives me:
? <-- HERE follows nothing in regex; marked by <-- HERE in m/ / at -e line 4.
Without the "x" this works fine.

So what gives?

I thought there was no problem using variables inside extended patterns, and couldn't find anything to the contrary in any documentation / web search either.

/|\

Replies are listed 'Best First'.
Re: Variables in extended patterns?
by JavaFan (Canon) on Nov 05, 2008 at 15:52 UTC
    You can use variables inside 'extended' patterns, but if you use 'extended' patterns, unescaped whitespace will be ignored. So, Perl happily interpolates a carriage return and a linefeed, and then, when it compiles and takes notice of the /x, it'll ignore them.

    Considering that

    s{$CR?$LF}
    doesn't contain any literal whitespace, the use of /x isn't quite needed. Furthermore, since you don't have a dot, there's no need for /s. And the absence of ^ and $ anchors make the /m superfluous as well.

    Now, if you have a modern Perl (say, one released last year, like 5.10), you could write:

    s/\R/\n/g;
    instead.
      Thanks.

      I missed that whitespace will be discarded after variable imterpolation.

      About just omitting xms: Of course that's exactly what I did, never mind what some clever guy calls "best practises".

      I'm now trying to find documentation on what, exactly, \R does... My perl 5.8.6 lists a few escape-sequences in "man perlop", section "quote and quote-like characters", but not \R.

        I said a modern perl. 5.8.6 is four years old. It doesn't have \R. 5.10 does. See 'man perlrebackslash' (yes, you need 5.10 for that as well)
        You can always write
        my $CRE = '0D'; my $LFE = '0A'; s{\x{$CRE}?\x{$LFE}}{\n}xms; s{\015?\012}{\n}xms; s{\x0D\x0A}{\n}xms;
        or
        my $CRE = '\x0D'; my $LFE = '\x0A'; $_ = qq,aod \x0D\x0A another \x0D\x0A no more,; s/$CRE?$LFE/--/msxg; print; __END__ aod -- another -- no more