develooper Front page | perl.perl5.porters | Postings from May 2003

RE: [PATCH] Extra newline in stringified qr/\#/x

Thread Previous | Thread Next
Orton, Yves
May 30, 2003 08:47
RE: [PATCH] Extra newline in stringified qr/\#/x
Message ID:
> As far as I can tell, the stringified form of a regexp is only useful
> for debugging purposes, to dump the inner contents of a regexp.

On the contrary. The ability to stringify a regex is a crucial aspect power
and flexibility of perls regexes.

Consider the following.... (conceptual demonstration):

my $iso_date_capture=qr!(\d{4})[-/\\](\d{2})[-/\\](\d{2})!;
(my $iso_date_match=$iso_date_capture)=~s/\((?:\?[msiox-]*\:)?/(?:/g;

print $iso_date_capture,"\n";
print $iso_date_match,"\n";

By being able to stringify a regex we can manipulate them and generate new
regexes. This is not debugging this is good practice. The example above
takes a regex that captures and produces a regex that doesnt capture
mechanically. This useful in that it means I dont have two slighlty similar
patterns to keep consitant, and it provides workarounds for Perls numerical
capture buffer naming. For instance e I have a routine that may take a
pattern as an argument, and I will embed that argument into a abigger regex
that captures. Now if the incoming regex has a capture in it then all the
cpatures I do will have their offsets screwed (amazingly in .Net they
actually have a useful adjunct to the regex syntax allowing you to name
captures so this wouldnt be a problem in C# (/me can hear the pitter patter
fingers working over time to change that _real_ fast... ;-)).  But by
stringifying the incoming regex and eliminating any captures it contains the
problem is avoided.

So it could actually be argued that the ability to stringify a regex is in
fact the only workaround for some of perls regexes weaker points. :-)

Although the fact is that stringification of Regexes in perl is partially
broken (or was the last time I looked at the source.) For instance 

#!perl -l
my $funky=bless qr/The invisible regex!/,"flintstone";
print ref($funky);
print "$funky";
print "The invisible regex!"=~$funky;
bless $funky,"Regexp";
print ref($funky);
print "$funky\n";
(?-xism:The invisible regex!)

Even the magic pointer is still there the stringification lookup is
hardcoded against the package 'Regexp'.

I put a patch together (and a module) ages ago, but I never polished them
enough to publish.

Anyway, just my $0.02


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About