Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Definitely go for the module approach. Though not that short as I would prefer, the code below demonstrate how you could achieve the effect of "automatically" stuffing your constants into the list of exports of your module.

The code for finding the constant names in the symbol table is almost abstract and with a little effort could be migrated into a generic module, leaving your constant module lighter.

package MyConstants; use strict; use warnings; require Exporter; # you may use other exporter modules our @EXPORT; # empty by now use constant FOO => 1; use constant BOO => 2; use constant BAR => 3; # ... #### here begins the code to find and populate the list of exports wit +h constant names our @GLOBAL_SYMBOLS; # these are not package constants, but Perl artif +acts BEGIN { @GLOBAL_SYMBOLS = qw(BEGIN EXPORT); } # $ans = is_constant($symbol_name); # # decides if a name is a package constant or not. # Checks if it is all upper-case and not in @GLOBAL_SYMBOLS sub is_constant { my $k = shift; return $k =~ /^[A-Z]+$/ && !grep { $_ eq $k } @GLOBAL_SYMBOLS } # push_constants(\@export, @symbols) # # Push @symbol names into @export if they satisfy is_constant sub push_constants { my $export_ref = shift; my @symbols = @_; for my $s (@_) { if ( is_constant($s) ) { #print "s: $s\n"; push @$export_ref, $s } } } BEGIN { # iterate the package stash my @keys = do { no strict 'refs'; keys %{__PACKAGE__ . '::'} }; push_constants(\@EXPORT, @keys); } print "export: @EXPORT\n"; #### here ends the code to find and populate the list of exports with +constant names 1;

In reply to Re: sharing symbols by ferreira
in thread sharing symbols by mungohill

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found