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

I'm studying Conway's OO Perl and I'm reading about blessed regular expressions.
my $middle = AltRegex->new(',(.*),');
When I step through the program with debugger, I try:
x $middle
...after $middle has been blessed and it tells me that it's a scalar reference that is undefined. And when I try to dereference $middle in my script to look at it, I get an error. Still, everything works fine, just as if $middle contained the RE given in the constructor's argument. Obviously, there is some magic going on with blessed REs beyond my comprehension. But can someone please tell me if and how it's possible to reveal the data of the $middle object?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: How to view contents of a blessed Regular Expression?
by ariels (Curate) on Jul 16, 2001 at 19:08 UTC
    Stringifying a regular expression seems to yield its string contents. So you could use that: (in the debugger)
    DB<3> $x = qr/.+2/ DB<4> x "$x" 0 '(?-xism:.+2)' DB<5> $y = bless \$x, 'Foo' DB<6> x $y 0 Foo=SCALAR(0x140263050) DB<7> x "$$y" 0 '(?-xism:.+2)'

    I've seen no documentation of this, so I wouldn't count on it. But it does work for me (with 5.005_03).

      Hmmm, doesn't work for me using ActiveState Perl 5.6.1. I wonder if it makes a difference that I'm blessing it in the context of an eval{} function like so:
      eval { bless qr/$pattern/, ref($class) || $class };
      I'm finding this all very confusing and I should probably just forget about such a minor thing.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

        qr// gives you a reference to a scalar that is already blessed into the Regexp package. So you are reblessing it which stops Regexp methods/overloads from working on it after that point. Stringification is a Regexp overload and so no longer works. If you want stringification again, then you'd need to provide such a overload via your AltRegex package.

                - tye (but my friends call me "Tye")