bikeNomad has asked for the wisdom of the Perl Monks concerning the following question:
The problem is that I often have multiple packages/classes defined in a single file (because they're small), and frequently have to share constants between them. However, there isn't a real easy way to inherit constants and still be able to use them in their bareword form (as you can in the package in which they're defined). The best I've been able to come up with is the following; can anyone point me to a better/cleaner way to do it? I'd rather not mess with Exporter, extra BEGIN blocks, etc.
#!/usr/bin/perl -w use strict; use vars qw(@ISA @EXPORT); package A; BEGIN { require Exporter; @A::ISA = 'Exporter'; @A::EXPORT = qw(CA sa); } use constant CA => 1; sub sa { my $a = CA; # fine } package B; BEGIN { A->import(':DEFAULT'); } @B::ISA = 'A'; sub sb { my $b = CA(); # fine my $c = CA; # fine, but only after import. }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Inheriting constants in the same file -- help me find a better way!
by tye (Sage) on Jun 25, 2001 at 09:02 UTC | |
|
Re (tilly) 1: Inheriting constants in the same file -- help me find a better way!
by tilly (Archbishop) on Jun 25, 2001 at 19:17 UTC | |
|
Re: Inheriting constants in the same file -- help me find a better way!
by bikeNomad (Priest) on Jun 25, 2001 at 20:58 UTC |