in reply to Re: calling subroutines within a regular expression?
in thread calling subroutines within a regular expression?

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).

  • Comment on Re^2: calling subroutines within a regular expression?

Replies are listed 'Best First'.
Re^3: calling subroutines within a regular expression?
by Bloodnok (Vicar) on Apr 04, 2009 at 10:07 UTC
    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
        You're not wrong (about the deprecation in favour of Readonly) ... and yes, I wholeheartedly agree with you WRT the elegance of the result.

        A user level that continues to overstate my experience :-))