in reply to Re: sharing symbols
in thread sharing symbols

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.

Replies are listed 'Best First'.
Re^3: sharing symbols
by ferreira (Chaplain) on Apr 13, 2007 at 19:42 UTC
    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.