in reply to Variables in extended patterns?

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.

Replies are listed 'Best First'.
Re^2: Variables in extended patterns?
by flamewise (Initiate) on Nov 05, 2008 at 16:19 UTC
    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