[Purdue-pm] don't use qr//o

Mark Senn mark at purdue.edu
Sat Feb 26 12:22:20 PST 2011


(I'll submit a bug report if this is in the next version of Perl.)

#!/usr/bin/perl

#
#  SUMMARY
#
#  Don't use qr//o.
#
#  DETAILS
#
#  This program works as expected.  It prints
#      a
#          1 yes
#          2 yes
#      b
#          1 yes
#          2 yes
#      c
#          1 no
#          2 no
#
#  If the
#      @re = map  { qr /$_/ }  @re;
#  line is changed to
#      @re = map  { qr /$_/o }  @re;
#  using
#      % perl --version
#      This is perl 5, version 12, subversion 3 (v5.12.3) [cont. on next line]
#      built for i386-linux-thread-multi
#  on
#      % cat /etc/fedora-release
#      Fedora release 14 (Laughlin)
#      % uname -a
#      Linux localhost.localdomain [cont. on next line]
#      2.6.35.6-48.fc14.i686 #1 SMP [cont. on next line]
#      Fri Oct 22 15:34:36 UTC 2010 i686 i686 i386 GNU/Linux
#  the program prints
#      a
#          1 yes
#          2 yes
#      b
#          1 no
#          2 no
#      c
#          1 no
#          2 no
#  Type
#      perl -Mre=debug broken.pl
#  where broken.pl is the program with the qr//o in it, to
#  see exactly why it fails.
#
#  .keywords
#      Perl
#      Perl qr
#      Perl qr//o
#  .ekeywords
#

use feature qw(say);

@re = qw(^a$ ^b$);
@re = map  { qr /$_/ }  @re;

@line = qw(a b c);

foreach $line (@line)
{
    say $line;



    # In Perl 5.10 here's one way to print "yes" or "no"
    # depending if a string matches any qr'ed regular expressions.

    $t = 0;

    foreach (@re)
    {
        ($line =~ $_)  and  $t = 1, last;
    }

    say '    1 ' . ($t ? 'yes' : 'no');



    # Get same results in Perl 5.12:

    say '    2 ' . ($line ~~ @re ? 'yes' : 'no');
}


More information about the Purdue-pm mailing list