Is there a way to precompile a regex in Perl? I have one that I use many times in a program and it does not change between uses.
相关问题
- $ENV{$variable} in perl
- Angular: ngc or tsc?
- Where is the implementation of included C++/C head
- Improve converting string to readable urls
- Is it possible to pass command-line arguments to @
相关文章
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Allow only 2 decimal points entry to a textbox usi
- Running a perl script on windows without extension
- Comparing speed of non-matching regexp
- Can NOT List directory including space using Perl
- Regular expression to get URL in string swift with
- Extracting columns from text file using Perl one-l
Simple: Check the qr// operator (documented in the perlop under Regexp Quote-Like Operators).
For literal (static) regexes there's nothing to do -- perl will only compile them once.
For regexes stored in variables you have a couple of options. You can use the
qr//
operator to build a regex object:This is handy if you want to use a regex in multiple places or pass it to subroutines.
If the regex pattern is in a string you can use the
/o
option to promise perl that it will never change:It's usually better to not do that, though. Perl is smart enough to know that the variable hasn't changed and the regex doesn't need to be recompiled. Specifying
/o
is probably a premature micro-optimization. It's also a potential pitfall. If the variable has changed using/o
would cause perl to use the old regex anyway. That could lead to hard to diagnose bugs.for clarification, you can user precompiled regex as:
It is documented in perlre but there are no direct examples.