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 with constant names our @GLOBAL_SYMBOLS; # these are not package constants, but Perl artifacts 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;