... if you put [a Regexp object value produced by the qr// operator] directly into the hash and do a ref on the key you pull out, the ref-type is lost.
That's because all hash keys are (pure) strings. See what you get from the statement
my %hash = ( [ 1, 'x', 99, ] => 'foo' );
The anonymous array reference has been stringized into something like "ARRAY(0xabc456)" and its ref-itude has been entirely lost. That's ok, because the =~ qr// m// s/// operators et al are quite happy to operate with strings as well as on strings. I don't understand why you need to know if a given string was "originally" a Regexp object or not. What difference does it make?
c:\@Work\Perl\monks>perl -wMstrict -le
"use Data::Dump qw(pp);
;;
my %test = (
qr{ f [eio]+ e }xms => 'regex 1',
qr{pure string} => 'regex 2',
'pure string' => 'string 1',
);
pp \%test;
;;
for my $str (
'a pure string it is, yes!',
'a pure string too',
'xxx fiiiie yyy',
) {
print qq{'$str'};
;;
for my $k (keys %test) {
die $k, ' not a pure string: reference ', ref $k if ref $k;
printf qq{$test{$k}: $k: };
print $str =~ /($k)/ ? qq{match, \$1 is '$1'} : 'NO match';
}
print '';
}
"
{
"(?^:pure string)" => "regex 2",
"(?^msx: f [eio]+ e )" => "regex 1",
"pure string" => "string 1",
}
'a pure string it is, yes!'
regex 1: (?^msx: f [eio]+ e ): NO match
string 1: pure string: match, $1 is 'pure string'
regex 2: (?^:pure string): match, $1 is 'pure string'
'a pure string too'
regex 1: (?^msx: f [eio]+ e ): NO match
string 1: pure string: NO match
regex 2: (?^:pure string): NO match
'xxx fiiiie yyy'
regex 1: (?^msx: f [eio]+ e ): match, $1 is 'fiiiie'
string 1: pure string: NO match
regex 2: (?^:pure string): NO match
... the '^' being inserted after '(?' ... Do you know why it was inserted in the stringified version?
From Perl version 5.14 on, this represents a default set of regex modifiers that can be altered by further modifiers; see (?^alupimsx) (update: and (?^aluimsx:pattern)) in Extended Patterns.
Update: I posted this reply before reading kcott's earlier reply. We've made a number of similar points, but one important point kcott made that I did not is "There are certain things about qr that are non-standard and non-intuitive..." It's possible to produce strings with the q// or qq// operators that are exactly equivalent in their effect to the stringization of the Regexp object produced by the qr// operator, but it can be tricky. One thing is certain: the automatic stringization of a Regexp object can always be used in place of the object by the =~ m// s/// operators with exactly the same effect.
Give a man a fish: <%-{-{-{-<
|