nneul has asked for the wisdom of the Perl Monks concerning the following question:

I have:

$pat = "something with (.*?) in it"; $re = qr/$pat/; if ( $line =~ $re ) (or /$re/, same results) { $1 has nothing in it here. nor does $& or $` if ( $line =~ /$pat/ ) { $1 has the matched text in it here } }

The matches will occur orders of magnitude less frequently than the number of uses of the pattern, so the above code still helps performance dramatically, but it's ugly. (I'm trying to avoid taking the step of generating the code and eval'ing when qr// seems to almost do the job.)

I'm using 5.8.1 built from cpan tarball on rh9.

Replies are listed 'Best First'.
Re: Is there any way to get at paren matches from qr//
by eserte (Deacon) on May 13, 2004 at 15:56 UTC
    This has to be a pilot error. I get expected results with various perls from 5.8.0 to 5.8.4.
      $line = "kernel: hda error 22"; $pat = "kernel: (.*?) error"; $re = /$pat/; if ( $line =~ $re ) { print "A: \$1 = $1\n"; if ( $line =~ /$pat/ ) { print "B: \$1 = $1\n"; } }
      output:
      infinity(77)>./test.pl 
      A: $1 = 
      B: $1 = hda
      
        In your second example you have
        $re = /$pat/;
        when you mean
        $re = qr/$pat/;
        With the qr added, it gives the expected output.
        Uh. Never mind. I see it now. Somehow I lost the qr//. Doh!