Rubin
Rubin

Reputation: 39

Perl's regular expression modifier

What does the following Perl regular expression modifer do?

/o

For example,

$string =~ /foo\(\"([^\)]*)\"\)/o)   

What does the /o mean?

Upvotes: 1

Views: 354

Answers (3)

Connor Hull
Connor Hull

Reputation: 153

It tells Perl to only compile the expression once. See What is /o really for?.

Upvotes: 6

ikegami
ikegami

Reputation: 385764

Absolutely nothing!

It affects interpolation, and nothing is being interpolated into that pattern.

Were there something interpolated, /o would cause the interpolation to only happen once, no matter how many times the match operator is executed.

>perl -E"for (['o','foo'],['a','bar'],['e','neo']) {
    my ($pat, $s) = @$_; say $s =~ /$pat/ ? $& : 0 }"
o
a
e

>perl -E"for (['o','foo'],['a','bar'],['e','neo']) {
    my ($pat, $s) = @$_; say $s =~ /$pat/o ? $& : 0 }"
o
0
o

Upvotes: 5

sergio
sergio

Reputation: 69027

The /o option for regular expressions (documented in perlop and perlreref) tells Perl to compile the regular expression only once. This is only useful when the pattern contains a variable. Perls 5.6 and later handle this automatically if the pattern does not change.

(source)

Upvotes: 5

Related Questions