Here's a little snippet to dump the constants declared by constant.pm in the current package:
sub dump_consts_in_current_package { no strict 'refs'; printf "%s => %s\n", $_, join ', ', $_->() foreach grep $_ =~ ('^' . __PACKAGE__ . '::'), keys %constant::d +eclared; }

A brief explanation of the code. Constants are special subroutines, check out perlsub, but can still be called as any other subroutine. I choose to treat it as a symbolic reference, and dereference it immediately with the arrow notation; see perlref. To get only the current package's constants I use grep() to filter out those that begin with the current package's name. The perhaps unusual pattern is nothing but an expression that's later interpreted as a pattern.

That's it. :)

(As a side-note: The grep() above won't be slow because of the "special" pattern. __PACKAGE__ is compile-time so a constant will be folded in, and two constants concatenated is optimized to be just one constant. This in combination with that identical patterns at the same place won't recompile makes this not impose any overhead. You could interpolate __PACKAGE__ and use the o modifier to make the pattern compile at program compile-time. (Update: If I interpolate it won't be precompiled anyway... so the only thing gained by using the o modifier is that reinterpolation won't occure.) But the o modifier doesn't work in activeperl, at least not my activeperl, so I never use it. (Update: Clarification per request: With the o modifier I mean the "compile only once" modifier you can put on pattern quote ops, like m/PATTERN/o.)

Hope I've helped,
ihb

In reply to Re: Accessing constants via symbol table possible? by ihb
in thread 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.