so, this is my first question here, I am ready to embarass myself... I would like to write a command template for my procedures, kinda like:
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 ... / ;
and then call an eval-procedure, later, when the variables are filled, e.g.
sub some_func { my $func = 'backup'; my $client = 'mars'; my $cmd = eval_cmd($func); # here magic happens! seurity_check($cmd); system("$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 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; }
does this make any sense, or what's a better way to achieve what I want? Oh, this is how I would use it:
# $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'); } }
Looking for suggestions...

--
I just can't seem to see the forest... all those trees are blocking my sight!

In reply to using a cmd-template by goldenblue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.