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

Re^2: Regular expression "replace string interpolation" problem

by cLive ;-) (Prior)
on May 16, 2008 at 22:25 UTC ( #687038=note: print w/replies, xml ) Need Help??

in reply to Re: Regular expression "replace string interpolation" problem
in thread Regular expression "replace string interpolation" problem

That works - thanks :)

But, reading through the regex documentation, I still can't work out exactly why. Hmmm.

so I guess needs to be \\" in the replace string for quotes. Arghhh, double escaping!!

  • Comment on Re^2: Regular expression "replace string interpolation" problem

Replies are listed 'Best First'.
Re^3: Regular expression "replace string interpolation" problem
by ikegami (Pope) on May 16, 2008 at 22:39 UTC

    Or how to embed " in the output - but that's not a biggie right now.

    You want to produce the code
    "$1 \"Perl\""
    my $replace = '"$1 \\"Perl\\""'; # Produces: "$1 \"Perl\"" s/$match/eval $replace/e

    Technically, the slashes don't need to be doubled since single quotes are forgiving when it's unambiguous.

Re^3: Regular expression "replace string interpolation" problem
by pc88mxer (Vicar) on May 17, 2008 at 06:36 UTC
    To explain what's going on it might be helpful to use underlined bold text to represent strings. So, one can write:
    eval 2+24
    and this means if you eval the string 2+2, you'll get the string 4. In perl this is just:
    my $x = '2+2'; my $y = eval $x; # $y is the string '4'

    In your original example, what is happening during the substitution s/$match/$replace/e is:

    eval $replace$1 Perl
    I.e., the match is replaced with the string $1 Perl, and that is why $text2 contains $1 Perl world.

    How about just adding another /e modifier to evaluate the substitution again? Unfortunately this doesn't work because $1 Perl world is not valid perl syntax:

    eval eval $replaceeval $1 Perl worldsyntax error

    When $replace and the substitution is written as:

    my $replace = '"$1 Perl"'; $text2 =~ s/$match/$replace/ee;
    the evaluation of the replacement proceeds as follows:
    eval eval $replaceeval "$1 Perl"
    and because of the double quotes this last eval yields $1 concatenated with a space and the string Perl.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://687038]
[LanX]: I'm not a big fan of pure make, apparently the auto generated ones are so complicated to be able to work with all possible makes
[stevieb]: I find the M::S makefiles it generates are quite straight forward, and I usually have to add a few things (github info etc). They're about 15 lines or so give or take.
[Corion]: I don't think the EUMM-generated Makefile is that complicated ;)
[stevieb]: Corion++
[jedikaiti]: RonW++ #I have so much accumulated in my memory that finding the right item at a given moment often fails
[LanX]: corion only > 800 lines
[stevieb]: ahhh you're talking about the actual Makefile, not the PP Makefile.PL

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-08-18 21:00 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.