Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: sharing symbols

by ferreira (Chaplain)
on Apr 13, 2007 at 10:42 UTC ( #609875=note: print w/replies, xml ) Need Help??


in reply to sharing symbols

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;

Replies are listed 'Best First'.
Re^2: sharing symbols
by bart (Canon) on Apr 13, 2007 at 11:42 UTC
    our @GLOBAL_SYMBOLS; # these are not package constants, but Perl artif +acts BEGIN { @GLOBAL_SYMBOLS = qw(BEGIN EXPORT); }
    sub is_constant { my $k = shift; return $k =~ /^[A-Z]+$/ && !grep { $_ eq $k } @GLOBAL_SYMBOLS }
    Bah, use a hash, Luke! Use a hash!
    our %GLOBAL_SYMBOL; # these are not package constants, but Perl artifa +cts BEGIN { %GLOBAL_SYMBOL = map { $_ => 1 } qw(BEGIN EXPORT ISA EXPORT_OK GLO +BAL_SYMBOL); }
    sub is_constant { my $k = shift; return $k =~ /^[A-Z_0-9]+$/ && !$GLOBAL_SYMBOL{$k}; }

    BTW did you forget to exclude @GLOBAL_SYMBOLS, by any chance? And, why not use a lexical for it?

    BTW I think this functionality is generally interesting enough to deserve its own module on CPAN. Let's call it ExportAll, or something else like that. And, as the helper subs would reside in that module, they wouldn't be exported themselves.

    Update: Oh no, I see you only support only upper case letters only names (which would exclude @GLOBAL_SYMBOLS). Which seems too restrictive to my taste.

      Oh no, I see you only support only upper case letters only names (which would exclude @GLOBAL_SYMBOLS). Which seems too restrictive to my taste.

      Don't go down on me that fast. This restriction can be lifted very easily and that's why I kept is_constant separated from push_constants. If the role of is_constant is taken by a subroutine ref, the rest is generic enough.

      Here an implementation of that generic module whose idea you cherished for a moment:

      And then applying it to the original request of the OP: The user's code just says
      use MyConstants;
      to import the constants into the current namespace.

      BTW I have not worried with @GLOBAL_SYMBOLS because it didn't match /^[A-Z]+$/ which does not accept underlines. It was an oversight, but, because of this, it was not a problem.

      BTW you were right about using a hash. The grep was only for a quick-and-dirty solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://609875]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2020-10-29 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (266 votes). Check out past polls.

    Notices?