Hrunting has asked for the wisdom of the Perl Monks concerning the following question:

Say I've got some code like this:
$class = 'Temp';
$re = qr/test/io;
my $regex = bless $re, $class;
After that regular expression is blessed, how do I know that it's a regular expression? I'm writing an in-house utility to dump perl objects and references to XML for transfer between some systems.

If I do '<kbd>ref( $re )</kbd>', I get 'Regexp', as I would expect, but when I do '<kbd>ref( $regex )</kbd>', I of course get 'Temp', and when I do '<kbd>scalar( $regex )</kbd>', rather then getting something useful like 'Temp=Regexp(?-ixsm:test)' like I might expect, I get something like 'Temp=SCALAR(0x00000)'. At this point, I have no way of knowing if that's a regular expression or a scalar ref or exactly what, and I thus can't dump it accurately.

Anyone have any clue how to get this information on blessed regular expressions?

For what it's worth, I checked Data::Dumper's output. It gives me something this:

$VAR1 = bless( do{\(my $o = undef)}, 'Temp' );

Replies are listed 'Best First'.
(tye)Re: Dumping Compiled Regular Expressions
by tye (Sage) on Nov 21, 2000 at 02:17 UTC

    The official solution is:

    UNIVERSAL::isa( $re, "Regexp" )
    but unfortunately UNIVERSAL::isa() is broken in this respect. Time to send in a patch (well, at least a bug report)!

            - tye (but my friends call me "Tye")
Re: Dumping Compiled Regular Expressions
by chromatic (Archbishop) on Nov 21, 2000 at 01:16 UTC
    If you set $class to a more complicated re, you'll see something interesting:
    my $class = 'Temp(a?)'; my $re = qr/test/io; my $regex = bless $re, $class; print "Verbatim: $regex\n"; print "Ref: ", ref($regex), "\n";
    The 'class name' section of the reference actually contains regex characters. I'm not aware of any other way to check this than looking for invalid package name characters in the ref. Sorry.

    Update: I completely misread the original question, and am now looking for a way to redeem myself by giving a *good* answer. :|

    Check this out:

    my $re = qr/(test)/io; print "Verbatim: $re\n"; print "Ref: ", ref($re), "\n";
    It displays correctly unless you bless it into another class. Of course, it matches in both the qr and the re-blessed form, which indicates to me that *something* keeps track of whether or not it's a regex, no matter what class name it keeps around.
      Hmm, I may not have stated my original question clearly enough. Basically, my script is going to get an object. That object could be blessed into any class (not a name of my choosing), and that object could be a blessed reference of any kind. Most of the time, that's not a problem, but apparently these two lines will produce almost exactly the same output (only addresses will differ):
      my $re = qr/temp/i; bless $re, $class;
      my $var = 'temp';   bless $var, $class;
      
      Both of those objects, when looked at using ref() will be of class '$class' (in this case, say 'Temp'). Both of those objects, when looked at using scalar(), will appear as something like '$class=SCALAR(0x00000)' ($class could be 'Temp' or it could be whatever $class was; again, that's up to the object's original class). I guess my real question is, is there some other way to obtain information on that blessed value that I'm missing?
Re: Dumping Compiled Regular Expressions
by Fastolfe (Vicar) on Nov 21, 2000 at 01:50 UTC
    Remember that you can only bless references. By blessing a scalar (a Regexp), I'm assuming it's converted into an empty reference, losing its value. Try something like this:
    $re = \qr/test/io; # now a *reference* to the regexp bless $re, $class; print ref($re); # Temp print ref($$re); # Regexp
      But $re doesn't hold a scalar in your first line! It holds a reference to the blessed regex.

      You can bless a reference to a reference, which is what I'm starting to prefer. Your way works just as well, but I think it's more clear to say:

      my $re = qr/(test)io; # added captures for the example my $class = 'Test'; my $regex = bless(\$re, $class); # bless a reference to the compile +d regex my $data = "testing!"; if ($data =~ /$$regex/) { print "Found $1!\n"; } print ref($regex), "\t", ref($$regex), "\n";
      Then how does it get blessed at all? Perl will return a run-time error if you try to bless something that's not a reference. Those lines above will print out:
      Temp
      
      
      ie. No 'Regexp'. The string that you could formerly get to by scalar( $re ) is no where to be found, but it still works.

      Not only does $re not lose it's value, it's still a valid regex 'reference' (is it truly a reference?) and you can still use it like:

      print "Woohoo!\n" if $string =~ $re;   # prints "Woohoo!\n"