Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Why dot does not match newline in oneliner but it does in script, with /s modifier?

by bliako (Abbot)
on Oct 24, 2025 at 14:10 UTC ( [id://11166585]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I can not understand why this works:

my $x = <<'EOX'; A { B 123 C xyz } EOX $x =~ s/A\s*\{.+?\}//s; print "x='$x'\n"; # empty

but this does not (linux terminal):

perl -pe 's/A\s*\{.+?\}//s' <<'EOX' A { B 123 C xyz } EOX # prints exactly what the input is

I have checked if there are mistaken shell interpolations to the oneliner script, but there are not, I think:

x='s/A\s*\{.+?\}//s' echo $x # s/A\s*\{.+?\}//s

Edit: the same problem if the oneliner operates on a file:

cat > infile<<'EOX' A { B 123 C xyz } EOX perl -pe 's/A\s*\{.+?\}//s' infile # or perl -i -pe 's/A\s*\{.+?\}//s' infile

Replies are listed 'Best First'.
Re: Why dot does not match newline in oneliner but it does in script, with /s modifier?
by hippo (Archbishop) on Oct 24, 2025 at 14:50 UTC

    You are running perl -p which by default processes each line in isolation, therefore your regex will never match because you don't have a line with both an opening and closing brace on it, for example.

    You could change the line-ender to null to switch this behaviour off:

    $ perl -lp0e 's/A\s*\{.+?\}//s' <<EOX > A { > B 123 > C xyz > } > EOX

    This gives the empty output as you initially expected.

    Update: See the discussion in the follow-up question Oneliner replace in-place adds extra spaces at the end of the file particularly in regards to the use of -l and how the order of these options is important.


    🦛

      indeed it does work, thank you. I came from sed who has difficulties handling multi-line chunks but got beaten by the defaults.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11166585]
Approved by hippo
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2026-05-11 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.