G'day goldenblue,

This is something of an XY Problem inasmuch as you've provided a solution idea without specifying the actual problem you are attempting to solve. Consequently, my response is based on guesses regarding the problem and is, therefore, somewhat generic.

One of the first things that leapt out at me was your use of symbolic references. If you follow that link, you'll find problems with your code: see what it says about package vs. lexical variables and that it's the "refs" stricture (of strict) that will complain about using symbolic references.

However, I'm not suggesting you fix those coding problems! Use of symbolic references can result in all sorts of problems and I recommend you avoid them unless you have a very good reason not to and know exactly what you are doing and why. For a more detailed discussion, read "Why it's stupid to `use a variable as a variable name' - Parts I, II and III" by Mark Jason Dominus.

Here's an alternative, general solution that may be better:

#!/usr/bin/env perl -l use strict; use warnings; my %cmd_gen = ( backup => sub { my ($bak_cmd, $client, $file) = @_; return [ $bak_cmd, $client, '>', $file ]; }, list => sub { my ($list_cmd, $client) = @_; return [ $list_cmd, '-le', $client ]; }, ); my $bak_cmd = 'some_backup_prog'; my $list_cmd = 'some_list_prog'; for my $client (qw{XXX YYY ZZZ}) { my $file = $client . '.bak'; my $backup_cmd_ref = $cmd_gen{backup}->($bak_cmd, $client, $file); #system(@$backup_cmd_ref); # for production print "@$backup_cmd_ref"; # for debugging my $list_cmd_ref = $cmd_gen{list}->($list_cmd, $client); #system(@$list_cmd_ref); # for production print "@$list_cmd_ref"; # for debugging }

Output:

some_backup_prog XXX > XXX.bak some_list_prog -le XXX some_backup_prog YYY > YYY.bak some_list_prog -le YYY some_backup_prog ZZZ > ZZZ.bak some_list_prog -le ZZZ

Some values (e.g. for $bak_cmd and $list_cmd) may be known in advance from configuration, environment variables, defaults, command line options, etc. In this case, your function calls would be simpler, e.g. $cmd_gen{list}->($client).

-- Ken


In reply to Re: using a cmd-template by kcott
in thread 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.