Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: use of constants in regex substitutions?

by dmitri (Priest)
on Sep 22, 2003 at 23:33 UTC ( [id://293327]=note: print w/replies, xml ) Need Help??


in reply to use of constants in regex substitutions?

You can treat constants as subroutines (which they are). In your case, the statement should look something like this:
$s1 =~ s/&TXT_EXTENSION/&MIF_EXTENSION/e;
Note the 'e' quantifier after the regex -- this tells the engine to evaluate the substitute before the substitution.

Update: this code is incorrect, thanks to 3dan for pointing this out.

Replies are listed 'Best First'.
Re: Re: use of constants in regex substitutions?
by edan (Curate) on Sep 23, 2003 at 11:45 UTC

    That won't work, because the /e modifier only applies to the second part of the substitution (i.e. the 'substitute with...' part). The first part of the regex follows normal string-interpolation rules, and "&sub" is NOT interpolated in a string any more than "sub" is. This regexp looks for the literal text '&TXT_EXTENSION', and substitutes it with '.mif'.

    See Zazo's reply for a correct answer.

    --
    3dan

      You're right; I should've tested first. How come &sub is interpolated when using arrow operator?
      &sub => "xyz";
      AFAIK, this is equivalent to
      "&sub", "xyz";

        Well, I believe the => operator forces a bareword to its left to be interpreted as a string. But &sub isn't a bareword it's a subroutine call, so it works like you want it to. At least, this is my understanding. Hopefully someone will correct me if I'm wrong.

        --
        3dan

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found