[sf-perl] trying to constantize a compiled regex

David Alban extasia at extasia.org
Wed Feb 20 18:18:22 PST 2008


greetings,

i'm trying to make a constant of a compiled regex.  i've done it
before, but i can't seem to make it work today.  the code below
prints:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa does not match $VAR1 = qr/(?-xism: m{
\A [a-f\d]{32} \z }xms )/;
11111111111111111111111111111111 does not match $VAR1 = qr/(?-xism: m{
\A [a-f\d]{32} \z }xms )/;
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa does not match $VAR1 = qr/(?-xism: m{
\A [a-f\d]{32} \z }xms )/;
11111111111111111111111111111111 does not match $VAR1 = qr/(?-xism: m{
\A [a-f\d]{32} \z }xms )/;
11111111111111111111111111111111 matches rgx

the test code producing the output is:

#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper;
use Readonly;

     # an md5sum should match this regular expression
Readonly::Scalar my $MD5SUM_RGX => qr{ m{ \A [a-f\d]{32} \z }xms };

my $s = 'a' x 32;

if ( $s =~ $MD5SUM_RGX ) {
  print "$s matches ", Dumper $MD5SUM_RGX;
} # if
else {
  print "$s does not match ", Dumper $MD5SUM_RGX;
} # if

$s = '1' x 32;

if ( $s =~ $MD5SUM_RGX ) {
  print "$s matches ", Dumper $MD5SUM_RGX;
} # if
else {
  print "$s does not match ", Dumper $MD5SUM_RGX;
} # if

     # try as a non-readonly variable
my $md5sum_rgx = qr{ m{ \A [a-f\d]{32} \z }xms };

$s = 'a' x 32;

if ( $s =~ $md5sum_rgx ) {
  print "$s matches ", Dumper $md5sum_rgx;
} # if
else {
  print "$s does not match ", Dumper $md5sum_rgx;
} # if

$s = '1' x 32;

if ( $s =~ $md5sum_rgx ) {
  print "$s matches ", Dumper $md5sum_rgx;
} # if
else {
  print "$s does not match ", Dumper $md5sum_rgx;
} # if

if ( $s =~ m{ \A [a-f\d]{32} \z }xms ) {
  print "$s matches rgx\n";
} # if
else {
  print "$s does not match rgx\n";
} # if

i can make the regex match,  but not as a readonly constant.  i'm sure
i'm missing something obvious...

thanks.

p.s.  dropping the 'ms' from the qualifiers doesn't change the output.

-- 
Live in a world of your own, but always welcome visitors.


More information about the SanFrancisco-pm mailing list