in reply to Dumping Compiled Regular Expressions

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

Replies are listed 'Best First'.
Re: Re: Dumping Compiled Regular Expressions
by chromatic (Archbishop) on Nov 21, 2000 at 01:57 UTC
    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";
Re: Re: Dumping Compiled Regular Expressions
by Hrunting (Pilgrim) on Nov 21, 2000 at 02:00 UTC
    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"