in reply to Re: Re: Code critique XS function for extracting a blessed regex's pattern.
in thread Code critique XS function for extracting a blessed regex's pattern.

My question is this: given an arbitrary blessed scalar ref, how does one efficiently determine if the object is in fact a regex?

Once you bless a Regexp into another class it isn't a Regexp anymore... try:

<update>As demerphq kindly pointed out I lied :-) Can you spot the silly mistake in the "demonstration" below :-)</update>

my $bqr=bless qr/^blessed$/,"Foo"; print "no match for $bqr\n" unless "normal" =~ m/$bqr/;

:-)

I guess you could subclass it (although this is something I've never tried) - in which case

UNIVERSAL::isa($qr, 'Regexp')

would be the right solution.

Replies are listed 'Best First'.
Re: Re: Re: Re: Code critique XS function for extracting a blessed regex's pattern.
by demerphq (Chancellor) on Feb 06, 2003 at 01:31 UTC
    Once you bless a Regexp into another class it isn't a Regexp anymore...

    Nope. The magic doesn't go away. As you can see.

    sub t { printf "%10s %s /%s/\n", $_[0], ($_[0]=~/$_[1]/ ? "=~" : "!="), $_[1]; }; $bqr=bless qr/^blessed$/,"Foo"; $qr=qr/^normal$/; foreach $rex ($bqr,$qr) { t($_,$rex) foreach qw(normal blessed); } __END__ normal != /Foo=SCALAR(0x1abf1d8)/ blessed =~ /Foo=SCALAR(0x1abf1d8)/ normal =~ /(?-xism:^normal$)/ blessed != /(?-xism:^normal$)/
    In fact i think its considered a feature. The possibilities are kinda interesting. :-)

    --- demerphq
    my friends call me, usually because I'm late....

      D'oh - learn something new every day! Thanks for putting this poor fool straight :-)

      (Moral - don't type code examples at 1am. Bad adrian)