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

Re^5: how to create a constant file and use it in other file

by alexm (Chaplain)
on Jul 17, 2008 at 01:51 UTC ( #698212=note: print w/ replies, xml ) Need Help??


in reply to Re^4: how to create a constant file and use it in other file
in thread how to create a constant file and use it in other file

It took me a while but I finally got something that works as you want:

##### MyConst.pm ##### package MyConst; use warnings; use strict; use Readonly (); require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(); Readonly::Scalar our $const1 => 1; Readonly::Array our @array => qw(x y z); Readonly::Hash our %hash => (abc => 123, def => 456); sub foo { 'foobar' } my $package = __PACKAGE__; no strict 'refs'; while (my $sym = each %{ "$package\::" }) { # skip internals next if $sym =~ /^(?:BEGIN|EXPORT|ISA)$/; if (defined ${ $sym }) { push @EXPORT, "\$$sym"; } elsif (defined %{ $sym }) { push @EXPORT, "\%$sym"; } elsif (defined @{ $sym }) { push @EXPORT, "\@$sym"; } elsif (defined &{ $sym }) { push @EXPORT, "$sym"; } } 1; __END__

Note 1: the position of our combined with Readonly is tricky. Using our Readonly::Scalar $const1 => 1 instead won't export $const1, but won't complain either.

Note 2: be careful to not import any other symbols from other modules (e.g. Data::Dumper exports Dumper by default).

##### test.pl ##### # Uncomment to see Exporter's magic #BEGIN { $Exporter::Verbose = 1 } use MyConst; use Data::Dumper (); use strict; use warnings; print Data::Dumper->Dump([$const1, \@array, \%hash], [qw(*const1 *arra +y *hash)]), "\n"; print 'foo() = ', foo(), "\n"; __END__

Thanks to Abigail's code referenced by zentara on Re: Listing the functions of a package / methods of an object.

Update: There's a simpler method, which is to export a hash and make everything to be exported a member of that hash, so there's only 1 element to export.


Comment on Re^5: how to create a constant file and use it in other file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-08-21 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (144 votes), past polls