Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Can't say in a regex?

by JadeNB (Chaplain)
on Nov 21, 2009 at 16:56 UTC ( #808603=perlquestion: print w/replies, xml ) Need Help??

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

“Why, yes,” you reply, “one can say in a regex”:

$ perl -E '/(?{ say "See?" })/' See?
“But,” I reply, “what if you want to interpolate a pattern?”
$ perl -E 'use re "eval"; my $a = qr//; /(?{ say("See?") })$a/' Undefined subroutine &main::say called at (re_eval 1) line 1.

UPDATE: ambrus suggested in the CB to remind the compiler that I want to use 5.010, and that works:

$ perl -E 'use re "eval"; my $a = qr//; /(?{ use 5.010; say "See?" })$ +a/' See?
which solves my problem, but seems strange. Why should the presence or absence of an empty regex require me to add an apparently superfluous use?
UPDATE: ambrus answered that, too. It's because the interpolated regex changes the big regex from a constant that can be compiled at compile time, to something not known to be constant that must be compiled at run time. Ah, well. I was too eager to post. :-)

Replies are listed 'Best First'.
Re: Can't say in a regex?
by ikegami (Pope) on Nov 21, 2009 at 18:07 UTC

    Pragmas (including the feature pragma) don't propagate into (?{}) and (??{}). It's a known bug.

    Example of strict and warnings not propagating:

    use strict; use warnings; use re 'eval'; my $re = ''; '' =~ /$re(?{ print(">$x<\n"); })/;

    Update: Added link and example

      Some further information, it's known in RT as bug 66104, and is on the list of "blocking bugs" for 5.12. But then, it was on the list of "blocking bugs" for 5.10.1 as well.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://808603]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2021-05-17 22:56 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (169 votes). Check out past polls.