in reply to Re: Re: Undefined regexp objects?
in thread Undefined regexp objects?

In fact... you can also call methods off of it and rebless the object into a different package and it keeps it's Regexp nature.

use strict; use warnings; my $r = qr/test/; print $r -> match( 'test' ) ? "Match\n" : "No match\n"; bless $r, 'beans'; print $r -> match( 'test' ) ? "Match\n" : "No match\n"; package beans; sub match { $_[1] =~ $_[0] } package Regexp; sub match { $_[1] =~ $_[0] }

Replies are listed 'Best First'.
Re: Re^2: Undefined regexp objects?
by ihb (Deacon) on Jan 20, 2003 at 13:21 UTC
    ... rebless the object into a different package and it keeps it's Regexp nature

    Not really. The Regexp nature will disappear, you bless away that. What won't disappear is the fact that the blessed thingy is a MG, i.e. magical. But the Regexp nature will disappear. For instance, you can no longer interpolate the pattern since the string overloading was done by Regexp. So /$re /x won't work the intended way. Note that for /$re/ no interpolation is done, so that still works as expected.

    ihb
      reblessed qr// will work as of 5.8.1.
Re: Re^2: Undefined regexp objects?
by steves (Curate) on Jan 16, 2003 at 01:10 UTC

    FYI, Damian Conway has a section on blessing regular expression references in his Object Oriented Perl book.

        Oh and see I'd thought that (prior to finding out you can bless them) that it might stomp all over the regexp magic since it's already one sort of object to begin with.