in reply to Re^2: how to use string RE (against $_ and capture)
in thread how to use string RE (against $_ and capture)

I tried using a "ref $re", looking for a Regexp (no go) ...

I'm not sure what this means. Again, my take:

c:\@Work\Perl\monks>perl -wMstrict -le "my $re_obj = qr{([0-6BS])}; if (ref $re_obj) { print 'A: reference: ', ref $re_obj; } else { print 'A: not a reference'; } ;; my $re_str = '' . $re_obj; if (ref $re_str) { print 'B: reference: ', ref $re_str; } else { print 'B: not a reference'; } " A: reference: Regexp B: not a reference


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: how to use string RE (against $_ and capture)
by perl-diddler (Chaplain) on Nov 08, 2016 at 02:10 UTC
    "tried using a 'ref $re', looking for a Regexp" means I tried using 'ref' on '$re' and expected to get a RegExp, but didn't, as in:
    my %hash=(qr{([0-4])}=>"foo", a=>"foo2"); $_=2; for my $re (keys %hash) { P "re=%s, ref=%s", $re, ref $re; P "match:(%s) %s", $1, $hash{$re} if /$re/; } ' re=a, ref= re=(?^:([0-4])), ref= match:(2) foo
    In the case that matches where "re=(?^:(0-4))", the ref was blank. I felt this was confusing -- since if you first assign the "qr" expression to a scalar and check the ref, you do get a ref of Regexp. But if you put it directly into the hash and do a ref on the key you pull out, the ref-type is lost. After the qr-expression has been used as a key in a hash, you lose the reference. At that point, determining that it was a Regexp, is a bit "unpretty", in that it seems one would test if the expression began with '(?'.

    Also, the '^' being inserted after '(?' looks odd since the original 'qr' expression didn't include it. Do you know why it was inserted in the stringified version?

    At this point, to check whether or not I need to use the 'key' directly or use it in a RE it seems I look for the paren, question-mark, which just looks strange, even though it works.

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

        Re: the default set of regex modifiers... ahhh.. forgot about that thanks for reminding me.