Recognizing a regex reference

F

Frank Seitz

use strict;
use warnings;

use Scalar::Util;

my $ref = qr/x/;
print 'Test1: ',ref($ref),"\n";
$ref = bless $ref,'X';
print 'Test2: ',ref($ref),"\n";
print 'Test3: ',Scalar::Util::reftype($ref),"\n";

__END__
Test1: Regexp
Test2: X
Test3: SCALAR

How do I recognize a regex reference?
ref() works only with unblessed references.
reftype() doesn't help.
Any ideas?

Frank
 
S

smallpond

use strict;
use warnings;

use Scalar::Util;

my $ref = qr/x/;
print 'Test1: ',ref($ref),"\n";
$ref = bless $ref,'X';
print 'Test2: ',ref($ref),"\n";
print 'Test3: ',Scalar::Util::reftype($ref),"\n";

__END__
Test1: Regexp
Test2: X
Test3: SCALAR

How do I recognize a regex reference?
ref() works only with unblessed references.
reftype() doesn't help.
Any ideas?

Frank

** Untested **

use Acme::Damn;
$tmp = $ref;
curse($tmp);
print 'Test4: ',ref($tmp),"\n";
 
S

smallpond

Quoth smallpond <[email protected]>:








What did you think this would do? Under all released versions of perl,
qr// returns a SCALAR ref, so this just prints "SCALAR". (Bizarrely,
this also seems to kill the stringify magic, which I don't understand at
all.)

Ben

I did not know that qr blessed its argument. It isn't
mentioned in perlop or perlre. My mistake.
 
F

Frank Seitz

Ben said:
No. All qr// refs are blessed, by default into class "Regexp" as you
just demonstrated. If they didn't have stringify magic they'd look like
Regexp=SCALAR(0xdeadbeef) when printed.


Under 5.8 and earlier, don't rebless qr// refs. There really isn't any
other answer, short of using B to poke around looking for 'r' magic, or
doing the equivalent in XS.

5.10 has re::is_regexp, which reliably identifies a qr//. 5.12 will have
a new REGEXP scalar type, so reftype will return "REGEXP" for qr// refs.

Thank you for the very well-informed answer.
If Regexp is the base class of all regexes, then $ref->isa('Regexp')
is the test I am looking for, i think. If qr// is blessed to another class,
this should be a subclass of Regexp of course.

Frank
 
F

Frank Seitz

Ben said:
What did you think this would do? Under all released versions of perl,
qr// returns a SCALAR ref, so this just prints "SCALAR". (Bizarrely,
this also seems to kill the stringify magic, which I don't understand at
all.)

The stringification is realized through a method. When the
class membership is removed from an object, then the stringification
method is lost, too.

Frank
 
I

Ilya Zakharevich

How do I recognize a regex reference?
ref() works only with unblessed references.

When I first implemented it, it must have been of class Regexp - for
performance reasons. It might have been that speed is not considered
an issue with newer perls...

Hope this helps,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top