Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by perlfan99 (Sexton)
on Jul 16, 2008 at 00:33 UTC ( #697835=note: print w/replies, xml ) Need Help??


in reply to Re^3: 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

is there a way to export all constants instead of listing each one of them? and also a way to importing all of in another file?
  • Comment on Re^4: how to create a constant file and use it in other file

Replies are listed 'Best First'.
Re^5: how to create a constant file and use it in other file
by sgifford (Prior) on Jul 16, 2008 at 02:36 UTC
    See the documentation for Exporter. Basically anything you list in @EXPORT_OK will be available for import by name into other modules which use the exporting module, and anything you list in @EXPORT will be imported automatically into useing modules. It's generally considered better style to use @EXPORT_OK, or to use a named tag in %EXPORT_TAGS, but if the whole purpose of your module is to provide constants @EXPORT may be OK.

    Your module might look something like this:

    package MyConst; use warnings; use strict; use base 'Exporter'; use vars qw(@EXPORT_OK %EXPORT_TAGS); @EXPORT_OK = qw($const1); %EXPORT_TAGS = (all => \@EXPORT_OK); # ...

    And the module using it would begin like this:

    use MyConst qw(:all);
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

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://697835]
help
Chatterbox?
[1nickt]: ... the end of the afternoon. marto How are you, snowed in? Mum and Dad expecting snow today in Surrey. (Have 5" on the ground here in NJ. And my son's high school in SOCal closed until after Xmas due to fires.)
[Corion]: 1nickt: In a sense the meta-bullshit might even be beneficial to you as you get to hear the cow-orkers talk (or not talk) about the issues you perceive. I think Agile is mostly about the meta ;)
[Corion]: 1nickt: On Sunday maybe 10cm of snow fell, and the airport closed, and the trains stopped running etc :) It always comes as a surprise that snow falls here during the winter...
[marto]: 1nickt, no snow, just some ice on the roads and the usual idiocy associated with humans and driving in less than ideal conditions

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-12-11 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (289 votes). Check out past polls.

    Notices?