Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value of `ref qr/.../`

Tags:

perl

ref:

But note that qr// scalars are created already blessed, so ref qr/.../ will likely return Regexp.

Does "likely" mean, that ref qr/.../ could also return something other than Regexp

like image 558
sid_com Avatar asked Sep 29 '21 04:09

sid_com


1 Answers

I think it's referring to the fact that someone could rebless the regex, warning that ref($something) eq 'Regexp' isn't 100% reliable.

use 5.010;

my $x = qr/a/;
say ref($x);

bless $x, "Foo";
say ref($x);

say "a" =~ /$x/;
Regex
Foo
1

On top of the above false negative, a false positive is also possible, since someone could bless something that's not a regex into Regexp. reftype is a better tool.

use 5.010;

use Scalar::Util qw( reftype );

my $re = bless(qr/a/, "Foo");
my $not = bless({}, "Regexp");

say ref($re),  " - ", reftype($re);
say ref($not), " - ", reftype($not);
Foo - REGEXP
Regexp - HASH
like image 78
ikegami Avatar answered Oct 03 '22 04:10

ikegami