1

I am in charge of upgrading this Perl script from Perl v5.6.1 (2001) to v5.20.2 (2015). I have these 2 regex variable:

foreach (@filelist) {
    chomp;
    my $File = $_;
    if ( $File =~ qr/.+/o ) {
        if ( $BaseLine ) {

            $BaseLineRegExpA = qr/^\Q$BaseLine\E\\/io; #these 2 regexes
            $BaseLineRegExpB = qr/^\Q$BaseLine\E;/io;  #these 2 regexes

            if ( $File =~ /$BaseLineRegExpA/ ) {
                #...

            } elsif ( (!($File =~ /$BaseLineRegExpB/)) && (!(lc( $File ) eq lc( $BaseLine ) )) ) {
                $BaseLine = $File;
            }
        }
     }
}

So, I have 2 questions:

  1. In the old Perl version, the $BaseLineRegExpA and $BaseLineRegExpB gets reevaluate every time $BaseLine changes, but in the new Perl, it does not. How do I make it changes? I've tried my $BaseLineRegExpA, it still does not change.

  2. In the old Perl, $BaseLineRegExpA evals to: (?i-xsm:^F:\\dd\\), and in the new Perl, it evals to (?^i:^F:\\dd\\). My questions is, is there a different between ?i-xsm:^ and ?^i:^?

Thanks so much, unfortunately, these are legacy scripts and I don't know much about Perl.

1 Answer 1

7
  1. The o modifier prevents re-evaluating variables substituted into regexes. It's curious that it didn't happen for you on 5.6, but it's probably because qr was still new in that version. Removing it (changing /io to /i) should make things work the way you expect.

  2. The (?i-xsm) encodes the regex modifier flags that are in effect (i is turned on, x, s, and m are turned off). Sometime around perl 5.14, Perl got some new regex modifier flags, which would change the stringification of all regexes. Since this was already a backwards-incompatible change, it was decided to do it in a way that would limit the hassle caused by adding any new flags down the road, and so the ^ character was used to represent the "default" set of flags. So (?^i) means "the default flags, plus the i flag". They both mean basically the same thing, and there's nothing you should worry about.

Sign up to request clarification or add additional context in comments.

2 Comments

Tested. qr//o was definitely broken in 5.6. The o was ignored.
@NamNgo you're welcome. Just as side note, I've been using Perl for 15 years and there isn't anyplace I would recommend using /o. At best it makes a very small performance improvement, easily outweighed by its potential for creating bugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.