goldenblue has asked for the wisdom of the Perl Monks concerning the following question:
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'); } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: using a cmd-template
by kcott (Archbishop) on Dec 09, 2013 at 15:24 UTC | |
by goldenblue (Acolyte) on Dec 11, 2013 at 09:14 UTC | |
by goldenblue (Acolyte) on Dec 11, 2013 at 11:32 UTC | |
Re: using a cmd-template
by VincentK (Beadle) on Dec 09, 2013 at 17:24 UTC | |
by goldenblue (Acolyte) on Dec 11, 2013 at 09:28 UTC |