in reply to Re: indirect/symbolic access in perl
in thread indirect/symbolic access in perl

Your last piece of code does not work - You are attempting to de-reference $flag, which yields undef, and prints zero.

This ugly code below does work:

#use strict; use Readonly; my @EXPORTS=qw(DBG_ONE DBG_TWO DBG_FOUR); Readonly::Scalar my $DBG_ONE => 1; Readonly::Scalar my $DBG_TWO => 2; Readonly::Scalar my $DBG_FOUR => 4; foreach my $flag (@EXPORTS) { printf "flag = %s, value = 0x%04x\n", $flag, eval( "\$" . $flag ); }
but I hate evals, and you would be much better served by using something like
Readonly::Hash my %DBG_Val => (DBG_ONE => 1, DBG_TWO => 2, DBG_FOU +R =>4);
Update:bruceb3's code works fine. Ignore the code I posted, which is essentially identical to bruceb3's but uses eval (and thus avoids conflict with "use strict 'refs'). Do follow the advice regarding using a Readonly HASH - much safer, and probably closer to what you need to do.

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Replies are listed 'Best First'.
Re^3: indirect/symbolic access in perl
by perl-diddler (Chaplain) on Sep 20, 2007 at 17:03 UTC
    re code working -- I did it from memory -- it was one of the iterations I went through while trying to find a workaround for the Readonly "bug" that was giving me a cryptic runtime message ("=28" isn't numeric")....