Perl s Modifier
In Perl, the s
modifier treats the string as a single line. This is the opposite of the /m modifier.
If you were to use this instead of /m
in the example in the lesson, it would only print the first word of the file.
/s option modifier
When using the /s option modifier in regular expressions, the "." matches also a newline character.
Suppose I want to remove the __END__ marker and all following lines from a file.
The following code should implement this:
use strict;
use warnings;
my $file = shift;
#file to process is the 1st command line argument
open my $fh, $file or die;
$_ = join '', <$fh>; # make file's content one (big?) line
close $fh;
s{__END__.*}{}s;
print;
Use the following solution.
open my $FH, '<', $file or die $!;
while (<$FH>) {
last if /^__END__\s/;
# Update: maybe /\b__END__\b/ or /(?:^|\s)__
+END__(?:\s|$)/)
print;
}
Also note that both the solutions incorrectly treat code like
print "
__END__
";
The pattern-matching operations m//, s///, and tr/// when used without an =~ operator.