Why is `stoutest` not a valid regex?

From perlop :

If "/" is a delimiter, then the initial value of m is optional. With m, you can use any pair of characters without spaces as separators. This is especially useful for matching pathnames containing "/" to avoid LTS (tooth tilt syndrome). If "?" is a separator, then the rule is only for uniformity? PATTERN? applies. If "'" is a delimiter, interpolation is not performed on PATTERN. When using a character valid in the identifier, a space after m is required.

So, I can choose any letter as a separator. In the end, this regex should be good:

 stoutest 

It can be rewritten

 s/ou/es/ 

However, it does not work in Perl. Why?

 $ perl -e '$_ = qw/ou/; stoutest; print' ou 
+6
source share
3 answers

Since Perl cannot select the s operator

perldoc perlop talks about it

Any separator without spaces can replace slashes. Add a space after s using the character allowed in the identifiers.

This program works great

 my $s = 'bout'; $s =~ s toutest; say $s; 

Output

 best 
+12
source

The replacement operator begins with the identifier s , but it is not in the code. Must use

 s toutest 

If this worked the way you think, we would not have any statements or routines starting with m , s , tr , q or y , since any delimiter without spaces can follow them.


Ironically, your own code proves why this is not the way you think. If it worked, do you think

 $_ = qw/ou/; stoutest; print 

will not be equivalent

 $_ = qw/ou/; s/ou/es/; print 

That would be equivalent

 $_ = q'/ou/; stoutest; print 

aka

 $_ = '/ou/; stoutest; print 
+4
source

Because stoutest or any other string of alphanumeric characters is the only token in the eyes of the Perl parser. Otherwise, we could not use a single word starting with s (or m , or q , or y ).

It works though

 $_ = "ou"; s toutest; print 
+4
source

All Articles