Home > other >  Return value of `ref qr/.../`
Return value of `ref qr/.../`

Time:09-30

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

CodePudding user response:

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
  •  Tags:  
  • perl
  • Related