in reply to Extracting regex from string

as an alternative which avoids the risks of eval try sprintf

$repl = 'hallo %s'; $string = sprintf $repl,$1,$2,$3,$4,$5,$6,$7,$8,$9;

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: Extracting regex from string
by AnomalousMonk (Archbishop) on Feb 19, 2017 at 20:17 UTC

    Also, the n$ "format parameter index" attribute of the sprintf format specifier can free you from the capture variable lockstep of $1 .. $n:

    c:\@Work\Perl\monks>perl -wMstrict -le "my $template = 'all the %3$s %1$s %2$s'; my $extract = 'all at sea in ships'; ;; if ($extract =~ m{ \A all \s+ (\w+) \s+ (\w+) \s+ in \s+ (\w+) \z }xm +s) { my $string = sprintf $template, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13; print qq{>$string<}; } " >all the ships at sea<
    Both safe and flexible.


    Give a man a fish:  <%-{-{-{-<