I have a silly problem.
In a script I defined many constants through the constant module.
I want to implement a function that dumps all those constants
when the script is invoked with a certain option.
To show you what I try to accomplish please have a look at the sample snippet below.
The only way I got access to the constants was through an eval(). But then it still fails if the constant holds a list of values.
I don't know if constants are at all part of the symbol table so my whole assumption of accessing them through symrefs may be absurd altogether.
Any suggestions?
#!/usr/bin/perl use strict; use constant { CLOUD => 9, HELL => 'sytem administration' }; use constant SINS => qw(pride envy gluttony lust anger greed sloth) +; dump_consts(); sub dump_consts { no strict 'refs'; print "SINS list:\t@{[SINS]}\n\n"; foreach my $const (keys %constant::declared) { printf "%-15s%6s => %s\n", 'eval:', $const, eval $const; printf "%-15s%6s => %s\n", 'symref:', $const, {$const}; printf "%-15s%6s => %s\n", 'symref fqn:', $const, {"constant::$con +st"}; printf "%-15s%6s => %s\n", 'symref hash:', $const, join(' ', values %{$const}); } }
This produces this output:
SINS list: pride envy gluttony lust anger greed sloth eval: main::CLOUD => 9 symref: main::CLOUD => HASH(0x4002da60) symref fqn: main::CLOUD => HASH(0x4002da60) symref hash: main::CLOUD => eval: main::HELL => sytem administration symref: main::HELL => HASH(0x4005c5f0) symref fqn: main::HELL => HASH(0x4005c5f0) symref hash: main::HELL => eval: main::SINS => pride symref: main::SINS => HASH(0x4005c5fc) symref fqn: main::SINS => HASH(0x4005c5fc) symref hash: main::SINS =>
In reply to Accessing constants via symbol table possible? by SIGSEGV
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |