Hi Perlmonks,

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

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.