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%.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |