mirod has asked for the wisdom of the Perl Monks concerning the following question:
I need to go back from the stringified form of a regexp to a real regexp. The regexp is stringified when I use it as the key to a hash (which I have to do as the current API dictates the use of a hash here).
The little bit of code below explains what happens and what I have done to get the regexp back.
#!/usr/bin/perl -w use strict; my $regexp = qr/^x/i; # a regexp # stringify it my $hash = { $regexp => 1 }; # keys are stringifi +ed my $string = (keys %$hash)[0]; # a string print "$string (ref: ", ref $string,")\n"; # and I prove it! # the string is: '(? +i-xsm:^x)' # regex-ify it if( $string=~m{^\(\?([xism]*)-([xism]*):(.*)\)$}) # match the interest +ing bits { my $nregexp= qr/(?$1:$3)/; # rebuild it print "$nregexp (ref: ", ref $nregexp,")\n"; # a regexp again! # it stringifies as +'(?-xism:(?i:^x))' }
My questions are: did I forget anything? Does it work accross versions of perl (I tested it under 5.8.6)? Is there anything simple that I forgot/did not know about?
|
|---|