RecursionBane has asked for the wisdom of the Perl Monks concerning the following question:
I wish to allow the user to craft any shell command that uses this variable, like so (again, simplified):> cat TEST_PACKAGE.pm package TEST_PACKAGE; use base 'Exporter'; our @ISA = qw(Exporter); our @EXPORT_OK = qw(%TEST_HASH); our %TEST_HASH; $TEST_HASH{'TEST_KEY'} = 'TEST_VALUE'; 1;
I've created multiple versions of this program to no avail:xterm> ./test.pl '/bin/touch $TEST_HASH{'TEST_KEY'}' # Would create + a touchfile called TEST_VALUE xterm> ./test.pl 'echo Test key contains $TEST_HASH{'TEST_KEY'}' # Wo +uld echo TEST_VALUE
In usage:> cat test.pl #!/usr/bin/perl my @command = @ARGV; use TEST_PACKAGE qw(%TEST_HASH); my $command_string; for (my $i = 0; $i < scalar(@command); $i++ ) { # Interpret each word in the command string and substitute kno +wn variables $command_string = $command_string . " " . ( sprintf "%s", $com +mand[$i] ); } print "\nExecuting command: $command_string\n"; system "$command_string";
I think the problem might be because Perl does not interpolate hash values inside quotes.xterm> ./test.pl 'echo Test key contains $TEST_HASH{'TEST_KEY'}' # O +ne of many trials, with varying escape characters tried Executing command: echo Test key contains $TEST_HASH{TEST_KEY} Test key contains {TEST_KEY}
|
|---|