Actually... I suppose, as a more direct answer to your question... you can do this:
%hash = ( # note the single-quotes wrapping the double-quotes... # I've defined a character string which is, itself, a valid # perl expression... which can be eval'd later command1 => ' "command_name -n $var1 -p $var2" ' ); $var1 = 'foo'; $var2 = 'bar'; # this is the "later" I promised above, when we eval # the character string which contains the perl expression system (eval $hash{'command1'});
The difference between this post and the parent post is actually only a very slight difference. This code is still, in essence, defining a subroutine reference, but it is not a closure. A closure implies static scoping, that is: the $var1 and $var2 which are visible at the time the closure is defined will be the $var1 and $var2 which are referenced at the time the code is executed. This example, however, uses dynamic scoping, that is: the $var1 and $var2 which get referenced when the code is executed are whatever variables named $var1 and $var2 are visible to the scope which executes the code. (static scoping=bound to the scope in which it was defined, dynamic scoping=scoped to wherever it is executed, whenever it is executed)

Anyway, in this very simple example, the two are functionally equivalent, but if you imagine that the hash is defined in a different scope than the value is asked for (=code is executed), then they will behave differently. Without your full code, I couldn't say which is the right one... but the dynamicaly scoped one is closer to the title of your question at least.

Aside: In college, I learned about dynamic lexical scoping, and thought someone would have to be on crack to use it, let alone implement it in a language. Then I met eval, and my life was forever changed.


------------
:Wq
Not an editor command: Wq

In reply to Re: Re: possible to evaluate scalars stored in a string? (dynamic scoping) by etcshadow
in thread possible to evaluate scalars stored in a string? by emilford

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.