Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:54:24+00:00 2026-05-22T22:54:24+00:00

Technical question: Given a regex: my $regEx = qr{whatever$myVar}oxi; # Notice /o for compile-once

  • 0

Technical question:

Given a regex:

my $regEx = qr{whatever$myVar}oxi; # Notice /o for "compile-once"

What is the most effective way to force it to recompile on demand? (e.g. when I know from the program logic that $myVar value changed) without dropping /o and depending on Perl’s internal smarts to auto-recompile?

NOTE: The regex is used in a substitution, which may affect re-compilation rules sans /o:

$string2 =~ s/$regEx//;

The context is:

  • I have a regular expression that is built by slurping in a fairly long (>1k long) string from a config file.

    • That file is re-read once every 60 minutes.

    • If the string read from the file changes (as defined by changing file timestamp), I want to re-compile the regex using the re-slurped string value in $myVar.

  • The regex is used repeatedly and frequently in the Perl module running under mod_perl.

    • This means that (coupled with the string being >1-2k long) I must use “/o” modifier to force compile-once on the regex, to avoid performance hit of Perl repeatedly checking if the variable value changed (this heuristic is from perlop qr//, since the regex is used as part of s/// as shown above and not by itself as a match).

    • That in turn means that, when I know that the variable changed after re-slurping it in 1 hour, I need to force the regex to re-compile despite the /o modifier.

UPDATE: Here’s an illustration of why I need /o – without it, the regex is recompiled (and thus necessarily checked) every loop iteration; with it it is NOT:

$ perl -e '{for (my $i=0; $i<3; $i++) {
                 my $re = qr{$i}oix; $s="123"; $s =~ s/$re//; 
                 print "i=$i; s=$s\n"; }}'
i=0; s=123
i=1; s=123
i=2; s=123

$ perl -e '{ for (my $i=0; $i<3; $i++) { 
                  my $re = qr{$i}ix; $s="123"; $s =~ s/$re//; 
                  print "i=$i; s=$s\n"; }}'
i=0; s=123
i=1; s=23
i=2; s=13
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:54:25+00:00Added an answer on May 22, 2026 at 10:54 pm

    when I know from the program logic that $myVar value changed

    m//, s/// and qr// only compile if the pattern doesn’t change. All you have to do to get the behaviour you requested is to remove the /o.

    $ perl -Mre=debug -e'
        qr/$_/ for qw( abc abc def def abc abc );
    ' 2>&1 | grep Compiling
    Compiling REx "abc"
    Compiling REx "def"
    Compiling REx "abc"
    

    Therefore,

    If the string read from the file changes (as defined by changing file timestamp), I want to re-compile the regex using the re-slurped string value in $myVar.

    my $new_myVar = ...;
    if ($myVar ne $new_myVar) {
       $re = qr/$new_myVar/;
       $myVar = $new_myVar;
    }
    ...
    s/$re/.../
    

    or just

    $myVar = ...;
    ...
    s/$myVar/.../
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not a technical question, but given that there are a few iPhone
A quick technical question- I have two queries that output some of the same
This question is not technical, but is still about programming. What is the funniest,
This question might not seem very technical but, out of curiosity,I want to know
This question is more philosophical than technical. I've trained myself as a web developer
Question What would be a good (ideally, technical) reason to ever program some non-trivial
Sorry for the non-technical title, but I think it summarizes my question well. If
Sorry for a badly-phrased question. I have a requirement coming from a non-technical person
My question is very simple: is there any way to (programmatically, technically, or manually)
My question is not technical. It's more of a philosophical and really down to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.