Here are the changes that I made to the first chunk code and the output.
bruce:1:~/tmp $ cat p.pl #!/usr/bin/env perl use strict; use warnings; use Readonly; my @EXPORTs = qw(DBG_ONE DBG_TWO DBG_FOUR) ; Readonly my $DBG_ONE => 1; Readonly my $DBG_TWO => 2; Readonly my $DBG_FOUR => 4; no strict 'refs'; foreach my $flag (@EXPORTs) { printf "flag = %s, value = 0x%04x\n", $flag, ${$flag}; } bruce:1:~/tmp $ ./p.pl Use of uninitialized value in printf at ./p.pl line 15. flag = DBG_ONE, value = 0x0000 Use of uninitialized value in printf at ./p.pl line 15. flag = DBG_TWO, value = 0x0000 Use of uninitialized value in printf at ./p.pl line 15. flag = DBG_FOUR, value = 0x0000
In pondering why the uninitialized value warnings were being displayed I remembered that you can't use symbolic references on my variables. So change the my to our and retry ...
#!/usr/bin/env perl use strict; use warnings; use Readonly; my @EXPORTs = qw(DBG_ONE DBG_TWO DBG_FOUR) ; Readonly our $DBG_ONE => 1; Readonly our $DBG_TWO => 2; Readonly our $DBG_FOUR => 4; no strict 'refs'; foreach my $flag (@EXPORTs) { printf "flag = %s, value = 0x%04x\n", $flag, ${$flag}; } bruce:1:~/tmp $ ./p.pl flag = DBG_ONE, value = 0x0001 flag = DBG_TWO, value = 0x0002 flag = DBG_FOUR, value = 0x0004
Much better.
In reply to Re: indirect/symbolic access in perl
by bruceb3
in thread indirect/symbolic access in perl
by perl-diddler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |