If I have the names of variables in an array, is it possible to print out Scalar variables with those names? Have tried a few variations but not had any luck. Seems like this should be "trivial" (I thought I'd done it before...a hallucination, maybe?). But something along the lines of:
my @EXPORTS=qw(DBG_ONE, DBG_TWO, DBG_FOUR); Readonly my DBG_ONE => 1; Readonly my DBG_TWO => 2; Readonly my DBG_FOUR => 4; foreach $flag (@EXPORTS) { printf "flag = %s, value = 0x%04x\n", $flag, ${$flag}; }
I've also tried getting the value using eval, but no luck with that either.

Thought defining the debug flags with something like:

my $bit=0; sub bit(){ my $s=2**$bit; ++$bit; $s; } sub dbg_flag ($) { my $flag=shift; eval sprintf ( "Readonly my \$$flag => 0x%04x push \@EXPORTS, \$$flag sub $flag () {\$_debug_ops & $\$flag}",$$flag ); } #then use it with something like @myflags=qw{DBG_ONE,DBG_TWO...}; foreach $flag (@myflags) { dbg_flag($flag); }
Even without the "sub" declaration in the eval, I kept getting a warning about push requiring a 2nd argument to push onto the first.

Not sure, but even if I did get the eval to work, I'd probably have to return it as a string from the creation routine and eval it while not in a sub -- possibly not in a "for loop" either -- so the definition would be outside the defining routine and outside the foreach loop. Might be able to concat evals together and execute whole bunch in one eval after building up code in the foreach loop...but never got that far not being able to use a variable name as an indirect name of a scalar. Thought this might get around my previously mentioned "'=28' not numeric" bug...but this wasn't any easier. *sigh*

I think I've been up a bit too long...:-)
Linda


In reply to indirect/symbolic access in perl by perl-diddler

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.