Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Interpolation of capture buffers not working when regex stored in variable

by ikegami (Pope)
on Jun 05, 2013 at 20:14 UTC ( #1037297=note: print w/replies, xml ) Need Help??


in reply to Interpolation of capture buffers not working when regex stored in variable

You're confusing string literals (code) and strings (values).

In the source, "$2$1" is a string literal. It results in a string made up of the values of $1 and $2.

my $p = "$2$1"; # my $p = $2.$1; # Doesn't change the value of $2 or $1.

In the source, "$ARGV[0]" is a string literal. It results in a string made up of the value of $ARGV[0].

my $p = "$ARGV[0]"; # my $p = "".$ARGV[0]; # Doesn't change the value of $ARGV[0].

If you want process the value of a variable for $1 and $2 and replace them, you are trying to implement a templating system. There's a one that matches your specs called String::Interpolate.

use String::Interpolate qw( interpolate); my $t = '$1$2'; or my $t = $ARGV[0]; my $p = interpolate($t);

Replies are listed 'Best First'.
Re^2: Interpolation of capture buffers not working when regex stored in variable
by jacklh (Initiate) on Jun 06, 2013 at 00:58 UTC
    Thank you very much! String::Interpolate worked in conjunction with "replace()" code used by methods 4 & 5. We new about the string literal vs string value, but we didn't know how to "cast" it (not mentioned in any of the docs), so we'd hoped Perl would figure it out from context like it does for so many other type conversions. You have saved us. Many thanks again.
      Here's the modified code for others reference:
      use String::Interpolate qw( interpolate ); ...[snip]... # Method 4: worked! (not global) #my ($before, $after) = $regex =~ m{/(.*)/(.*)/}; #$filename = replace($filename, $before, sub {interpolate($after)},0); # Method 5: worked! (global) my ($before, $after) = $regex =~ m{/(.*)/(.*)/}; $filename = replace($filename, $before, sub {interpolate($after)},1);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1037297]
help
Chatterbox?
[marto]: good morning all
[Corion]: Hi marto!
[Corion]: I just found out that in $current_top_prior ity_project , the part I am in is not even in the top three worries. That's somewhat bad, not because I'm happy with being a top worry but because that means that I don't even know how bad the rest ...
[Corion]: ... of the situation is :-)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-07-26 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (385 votes). Check out past polls.