in reply to Undefined regexp objects?

qr// doesn't return a reference. Try:
use strict; use warnings; my $r = qr/Delectable/; print $r;
Update: ihb is quite right. I should have qualified that as scalar reference.

Replies are listed 'Best First'.
Re: Re: Undefined regexp objects?
by ihb (Deacon) on Jan 15, 2003 at 23:53 UTC
    qr// does indeed return a reference, try
    print ref(qr//)
    This object stringifies through, so you can interpolate it into other patterns.

    ihb

      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] }
        ... 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

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