FAQ 6.9 What is "/o" really for?
- From: PerlFAQ Server <brian@xxxxxxxxxxxxxx>
- Date: Sun, 30 Apr 2006 00:03:02 -0700
This is an excerpt from the latest version perlfaq6.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .
--------------------------------------------------------------------
6.9: What is "/o" really for?
Using a variable in a regular expression match forces a re-evaluation
(and perhaps recompilation) each time the regular expression is
encountered. The "/o" modifier locks in the regex the first time it's
used. This always happens in a constant regular expression, and in fact,
the pattern was compiled into the internal format at the same time your
entire program was.
Use of "/o" is irrelevant unless variable interpolation is used in the
pattern, and if so, the regex engine will neither know nor care whether
the variables change after the pattern is evaluated the *very first*
time.
"/o" is often used to gain an extra measure of efficiency by not
performing subsequent evaluations when you know it won't matter (because
you know the variables won't change), or more rarely, when you don't
want the regex to notice if they do.
For example, here's a "paragrep" program:
$/ = ''; # paragraph mode
$pat = shift;
while (<>) {
print if /$pat/o;
}
--------------------------------------------------------------------
The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.
If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
*** Posted via a free Usenet account from http://www.teranews.com ***
.
- Prev by Date: Re: perl addition operator. query.
- Next by Date: Re: random array elements and speed
- Previous by thread: Fields won't add when reading a text file
- Next by thread: FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl?
- Index(es):
Relevant Pages
|