Paul <[EMAIL PROTECTED]> writes: > You just needed an eval(). > This works: > > #!/dart03/users/bin/perl -wp > BEGIN { our($in,$out,$arg) = (shift,shift,shift||'') } > eval "s/$in/$out/$arg"; > > The -p means print each line after processing; it puts the input from > STDIN into $_. The BEGIN{} block gets your arguments, and I allowed for > a third to place modifiers such as g or e, but made sure it was defined > with at least an empty string with shift||''. The eval puts the pieces > into working order. :)
Thanks for the good tip Paul. I'm still having trouble integrating this new knowledge into my actual script though. Its about 350 lines long so I've attempted to show a greatly shortened version that has some more of the main elements of the actual script. Using the -p flag isn't needed here I think but having a time of it trying to get the eval to work where and as expected. I've, no doubt, thoroughly slaughtered good perl coding here. But what I'm asking is how to work the eval into more complex code like below, so that when -s option is used the lines of SRC_FILE are run through the s/$var1/$var2/$var3 editing tool and those vars are set by shifting off the args to -s. cat infile one blab blab two fee fi foo three something somewhere sometime four nobody anywhere anytime cat eval2.pl: #!/usr/local/bin/perl -w $rc_file = "./rc_file"; ## ========== BEGIN Getopts section ========== ## Declare vars inside qw() use vars qw($opt_a $opt_b $opt_s); use Getopt::Std; my $optstr ="abs:"; getopts($optstr); if ($opt_a) { print "opt_a hit\n"; $opt_a_hit = "TRUE"; $a = "Using a - "; } if ($opt_b) { print "opt_b hit\n"; $opt_b_hit = "TRUE"; $b = "Using b -"; } if ($opt_s) { $strp_re = $opt_s; print "opt_s hit\n"; $opt_s_hit = "TRUE"; $s = "Now run the eval stuff "; # BEGIN { our ($rein_str,$modifier) = (shift,shift||'') # }; } slurp_rc(); sub slurp_rc { open(RC_FILE,"<$rc_file"); while (<RC_FILE>) { chomp; push @files_to_search,$_; } close(RC_FILE); } for (@files_to_search) { $src_file = $_; open (SRC_FILE,"<$src_file"); while (<SRC_FILE>) { if ($opt_a_hit) { print "$a $_"; } if ($opt_b_hit) { print "$b $_"; } if ($opt_s_hit) { print "$s\n"; print "$strp_re $rein_str $modifier\n"; eval "$_ =~ s/$strp_re/$rein_str/$modifier"; } } close(SRC_FILE); } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]