#!/usr/bin/perl @lines = split $/, <<EOL; beginend begin1+0end begin1+1endbegin' $ENV{HOME}'end begin1+2end begin1+3end begin sprintf q~%.05\d~, 5endbegin sprintf q~%.05\d~, 6end EOL for (@lines) { s~begin(.*?)end~eval $1~ge ; print "$_$/" ; }
At 1:20 am -0700 28/8/03, Nicholas Thornton wrote:
I have a regex I'm performing on a file that takes a given string, manipulates it, and replaces the string with the new string. I want this do be done recursively on each line in case there are multiple sections that need to be dealt with. I'm currently using something like:
while(<$file>){ WHILE:while() { if ($string =~ /begin(.*?)end/) { my $perl = $1; my $eval = eval $perl; $string =~s/begin.*?end/$eval/; } else {last WHILE} } }
It looks sort of clunky to me, and I don't trust the subsitution even if it does work, but I can't seem to get it to work using $perl or $1 in place of the /.*?/. I was wondering if anyone had any suggestions for making it nicer, shorter, or cleaner?