#!/usr/bin/env perl use strict; use warnings; ### This eval code snippet works ### eval(my $ref2 = "parent_ref"); #################################### my $string = "first_string ref[i] second_string $ref2.ref[i] third_string"; my $new_string; my $i; for ($i=0; $i<5; $i++) { print "$i : "; $new_string = $string; # don't clobber original string, else search-and-replace fails after first loop $new_string =~ s/\[i\]/\[$i\]/g; print "$new_string\n"; } #### 0 : first_string ref[0] second_string parent_ref.ref[0] third_string 1 : first_string ref[1] second_string parent_ref.ref[1] third_string 2 : first_string ref[2] second_string parent_ref.ref[2] third_string 3 : first_string ref[3] second_string parent_ref.ref[3] third_string 4 : first_string ref[4] second_string parent_ref.ref[4] third_string