and then call an eval-procedure, later, when the variables are filled, e.g.my $cmd_tmpl = { backup => '$bak_cmd $client > $file', list => '$list_cmd -le $client', ... } # all the names of the vars to be substituted my @all_my_vars = qw / bak_cmd client file list_cmd ... / ;
basically, I want the eval_cmd function to go through my hash, pick the correct key and do a regexp over the variables... something like:sub some_func { my $func = 'backup'; my $client = 'mars'; my $cmd = eval_cmd($func); # here magic happens! seurity_check($cmd); system("$cmd"); }
does this make any sense, or what's a better way to achieve what I want? Oh, this is how I would use it:sub eval_cmd { my $key = shift; die "no such command: $key!" unless exists $cmd_template{$key}; my $cmd = $cmd_template{$key}; no strict "vars"; for my $var (@all_my_vars) { $cmd =~ s/\$var/$$var/g if defined $$var; } use strict "vars"; return $cmd; }
Looking for suggestions...# $client can be given, or not if ($client) { some_func(); } else { foreach $client (@{get_all_running_clients()}) { my $backup-cmd = eval_cmd('backup'); system('$backup-cmd'); } }
In reply to using a cmd-template by goldenblue
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |