perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:
I've also tried getting the value using eval, but no luck with that either.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}; }
Thought defining the debug flags with something like:
Even without the "sub" declaration in the eval, I kept getting a warning about push requiring a 2nd argument to push onto the first.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); }
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: indirect/symbolic access in perl
by bruceb3 (Pilgrim) on Sep 20, 2007 at 12:22 UTC | |
by NetWallah (Canon) on Sep 20, 2007 at 14:16 UTC | |
by perl-diddler (Chaplain) on Sep 20, 2007 at 17:03 UTC | |
by perl-diddler (Chaplain) on Sep 20, 2007 at 17:09 UTC |