Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

calling subroutines within a regular expression?

by Anonymous Monk
on Apr 03, 2009 at 22:27 UTC ( [id://755355]=perlquestion: print w/replies, xml ) Need Help??

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

The syntax appears to be thornier than what I am imagining.

I have the situation where the same regular expressions are needed throughout the code. To prevent future errors, it would be good to centralize the expression such that changes only need to be made once (and know about it...). The following code is syntactically incorrect, so what do I need to do in order to get the desired result?

Any insight would be appreciated

#!/bin/env perl use constant REGEX => qr/cat/; my $s = 'The cat in the hat...'; if ($s =~ /REGEX/) { print "found\n"; } else { print "not found\n"; }

Replies are listed 'Best First'.
Re: calling subroutines within a regular expression?
by runrig (Abbot) on Apr 03, 2009 at 22:39 UTC
    if ($s =~ /${\REGEX}/) {
    or:
    my $re = REGEX; if ( $s =~ /$re/ ) {
    But then what's the point of using constant? May as well:
    my $REGEX = qr/cat/; #... if ( $s =~ /$REGEX/ ) {

      One more for "fun"-

      if ( $s =~ /${+REGEX}/ ) { ...
        This doesn't seem to work:
        >perl -wMstrict -le "use constant RX => qr{ [atc]{3} }xms; my $s = 'the cat in the hat'; print 'found' if $s =~ /${+RX}/; print qq{found a $1} if $s =~ /(${+RX})/; " Use of uninitialized value in regexp compilation ... found Use of uninitialized value in concatenation (.) or string ... found a
      And then there is the always useful (if ever-homely):
      >perl -wMstrict -le "use constant RX => qr{ [atc]{3} }xms; my $s = 'the cat in the hat'; print qq{found a $1} if $s =~ m{ ( @{[ RX ]} ) }xms; " found a cat
Re: calling subroutines within a regular expression?
by SankoR (Prior) on Apr 03, 2009 at 22:43 UTC
    Hmmm... Seeing as REGEX is a regular expression in your example, why not just...
    # ... if ($s =~ REGEX) { print "found\n"; } # ...
Re: calling subroutines within a regular expression?
by Bloodnok (Vicar) on Apr 03, 2009 at 22:51 UTC
    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 :-))
      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 :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://755355]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-24 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found