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.
Please keep in mind that I have greatly simplified the inputs and outputs in order to make it easy for you to read.

This package contains many hashes (simplified below to only contain one hash with one key):
> 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 wish to allow the user to craft any shell command that uses this variable, like so (again, simplified):
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
I've created multiple versions of this program to no avail:
> 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";
In usage:
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}
I think the problem might be because Perl does not interpolate hash values inside quotes.
Am I going about this the right way? Any suggestions from your collective wisdom would be much appreciated.

~RecursionBane

In reply to Perl as a command executor (with hash variable substitution) by RecursionBane

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.