Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the 'o' modifier for Perl regular expressions still provide any benefit?

Tags:

regex

perl

It used to be considered beneficial to include the 'o' modifier at the end of Perl regular expressions. The current Perl documentation does not even seem to list it, certainly not at the modifiers section of perlre.

Does it provide any benefit now?

It is still accepted, for reasons of backwards compatibility if nothing else.


As noted by J A Faucett and brian d foy, the 'o' modifier is still documented, if you find the right places to look (one of which is not the perlre documentation). It is mentioned in the perlop pages. It is also found in the perlreref pages.

As noted by Alan M in the accepted answer, the better modern technique is usually to use the qr// (quoted regex) operator.

like image 201
Jonathan Leffler Avatar asked Feb 15 '09 03:02

Jonathan Leffler


People also ask

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

What regex engine does Perl use?

Perl Compatible Regular Expressions (PCRE) is a library written in C, which implements a regular expression engine, inspired by the capabilities of the Perl programming language. Philip Hazel started writing PCRE in summer 1997.

What is S in Perl regex?

The Substitution Operator The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/;

What are regex modifiers?

Regular expression patterns are often used with modifiers (also called flags) that redefine regex behavior. Regex modifiers can be regular (e.g. /abc/i ) and inline (or embedded) (e.g. (? i)abc ). The most common modifiers are global, case-insensitive, multiline and dotall modifiers.


2 Answers

/o is deprecated. The simplest way to make sure a regex is compiled only once is to use use a regex object, like so:

my $reg = qr/foo$bar/; 

The interpolation of $bar is done when the variable $reg is initialized, and the cached, compiled regex will be used from then on within the enclosing scope. But sometimes you want the regex to be recompiled, because you want it to use the variable's new value. Here's the example Friedl used in The Book:

sub CheckLogfileForToday() {   my $today = (qw<Sun Mon Tue Wed Thu Fri Sat>)[(localtime)[6]];    my $today_regex = qr/^$today:/i; # compiles once per function call    while (<LOGFILE>) {     if ($_ =~ $today_regex) {       ...     }   } } 

Within the scope of the function, the value of $today_regex stays the same. But the next time the function is called, the regex will be recompiled with the new value of $today. If he had just used:

if ($_ =~ m/^$today:/io) 

...the regex would never be updated. So, with the object form you have the efficiency of /o without sacrificing flexibility.

like image 51
Alan Moore Avatar answered Sep 19 '22 15:09

Alan Moore


The /o modifier is in the perlop documentation instead of the perlre documentation since it is a quote-like modifier rather than a regex modifier. That has always seemed odd to me, but that's how it is. Since Perl 5.20, it's now listed in perlre simply to note that you probably shouldn't use it.

Before Perl 5.6, Perl would recompile the regex even if the variable had not changed. You don't need to do that anymore. You could use /o to compile the regex once despite further changes to the variable, but as the other answers noted, qr// is better for that.

like image 45
brian d foy Avatar answered Sep 22 '22 15:09

brian d foy