in reply to Interpolation of variables read from a file
Basically, you want a templating system. If you want to keep using the Perl hash syntax, I recommend the following approach:
my %act1 = ( "food" => "eat it", "drink" => "drink it", "Perl" => "play it"); my %act2 = ( "food" => "prepare it", "drink" => "be drunk", "Perl" => "write it"); my %actions = ( act1 => \%act1, act2 => \%act2, ); my %vars = ( item => \$item, ); my $out = '-p $act1{$item} -p $act2{$item} -p ... '; # resp. read that one from a file # The world's simpled templating system: $out =~ s-\$(\w+) # the name of the hash (%act1) \{ # opening brace \$(\w+) # the name of the variable to be used ($item) \} # closing brace - { my $useritem = $vars{ $2 }; $actions{$1}{$useritem} }gxe;
I haven't tested the code, so likely there are some syntax errors and errors with the specification of the regular expression - I hope that the comments help you enough to fix these.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Interpolation of variables read from a file
by phio (Acolyte) on Dec 04, 2007 at 10:09 UTC |