I suspect your key issue is that you are expecting that the variables in the command strings will get substituted at the time system is called. They don't. They get substituted at the time the hash is constructed. Instead you may want to use place holders and substitute the actual parameters in at system execution time. There are many ways you could achieve that - here's one:

use strict; my %systemcmd = ( add => "addqueue -h P0 -q P1 -i 3", remove => "removequeue -f -q P0", status => "lpstat -pP0", cancel => "cancel -e P0", add_slurp => "addqueue -h P0 -q P1 -i 3" ); my $file = <<FILE; add wibble ping cancel wobble flibble one FILE open INFILE, '<', \$file; while (<INFILE>) { chomp; next unless length; my ($command, @params) = split; unless (exists $systemcmd{$command}) { warn "Unrecognised command: $command in line $.\n"; next; } my $cmdline = $systemcmd{$command}; $cmdline =~ s/(P$_)/$params[$_]/e for 0 .. $#params; print qq{system ("$cmdline");\n}; } close INFILE;

Prints:

system ("addqueue -h wibble -q ping -i 3"); Unrecognised command: flibble in line 4 system ("cancel -e wobble");

Aside from that there are some major issues in your code. In particular you use the variable $file in about six different ways. The intent of your code is not at all clear and the chance that it is correct is 0%.


Perl is environmentally friendly - it saves trees

In reply to Re: Passing info from an array to a system call. by GrandFather
in thread Passing info from an array to a system call. by misconfiguration

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.