in reply to calling subroutines within a regular expression?

You might want to have a look at Regexp Quote Like Operators

... takes deep breath and prepares to put foot firmly in mouth ... you might also consider using a scalar (c/w a constant) to hold the pre-compiled regexp - I'm not altogether convinced that perl interpolates constant values within a regexp operation ... but I'm sure our (far) more learned monks will put me right in due course ...

A user level that continues to overstate my experience :-))
  • Comment on Re: calling subroutines within a regular expression?

Replies are listed 'Best First'.
Re^2: calling subroutines within a regular expression?
by chromatic (Archbishop) on Apr 04, 2009 at 01:57 UTC
    I'm not altogether convinced that perl interpolates constant values within a regexp operation ... but I'm sure our (far) more learned monks will put me right in due course ...

    What stops you from trying it yourself? That's what I'd do (though I'm not sure which particular flavor of Perl "constant" you mean there, so I won't post code).

      I did indeed try it myself some while ago, but eventually gave up on account of 2 things:
      • I couldn't get interpolation of constants declared using (pun intended) the constant module and then ...
      • I read PBP - which denegrates the use (again, pun intended) of the constant module

      In retrospect, it occurs to me that I used the latter as a convenient excuse/motive for giving up on attempts to achieve the former.

      A user level that continues to overstate my experience :-))
        IIRC, PBP deprecates the use of constant in favor of the Readonly module, which gives you (among other things) read-only scalars which interpolate as expected in double-quotish things like strings and regexes.

        I can't test this right now, but I think the following works and also gives you a more elegant regex interpolation:

        use warnings; use strict; use Readonly; Readonly my $BAR => qr{ bar }xms; my $string = q{fie foo bar baz barf}; print qq{found $1} if $string =~ m{ foo \s* ($BAR) \s* baz }xms;
        Should print:
        found bar