perlquestion
RecursionBane
Greetings, Monks!
I am trying to solve a problem with a rather stringent set of limitations. Having tried fruitlessly for several days, I turn to your wisdom to accomplish this.
<br>
Please keep in mind that I have greatly simplified the inputs and outputs in order to make it easy for you to read.
<br><br>
This package contains many hashes (simplified below to only contain one hash with one key):
<code>
> 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;
</code>
I wish to allow the user to craft any shell command that uses this variable, like so (again, simplified):
<code>
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'}' # Would echo TEST_VALUE
</code>
I've created multiple versions of this program to no avail:
<code>
> 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 known variables
$command_string = $command_string . " " . ( sprintf "%s", $command[$i] );
}
print "\nExecuting command: $command_string\n";
system "$command_string";
</code>
In usage:
<code>
xterm> ./test.pl 'echo Test key contains $TEST_HASH{'TEST_KEY'}' # One of many trials, with varying escape characters tried
Executing command: echo Test key contains $TEST_HASH{TEST_KEY}
Test key contains {TEST_KEY}
</code>
I think the problem might be because Perl does not interpolate hash values inside quotes.
<br>
Am I going about this the right way? Any suggestions from your collective wisdom would be much appreciated.
<br><br>
~RecursionBane