... 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:  <%-{-{-{-<


In reply to Re^5: how to use string RE (against $_ and capture) by AnomalousMonk
in thread how to use string RE (against $_ and capture) by perl-diddler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.