export constants nicely from perl module...
Michael G Schwern
schwern at pobox.com
Fri Feb 6 13:53:28 CST 2004
On Fri, Feb 06, 2004 at 06:49:53AM -0500, Wesley Darlington wrote:
> I resent having to give the name of each constant twice. (I'm going to
> call this 'syntactic soot' - better descriptions welcome!)
>
> I'm wondering if there's a better way. Perhaps something like...
>
> -----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
> package Widget::Config;
> use base 'Exporter::Constants';
>
> econst widget_dir => '/usr/local/widget/conf';
> econst max_widgets => 42;
> -----8<-----8<-----8<-----8<-----8<-----8<-----8<-----
>
> ...but as long as its syntactically reasonably sweet, I don't mind
> what it looks like. There will be lots of constants.
Remember, constants are just subroutines with a prototype of ().
Also, constants have to be declared at compile time in order to be constant,
but compile time is relative. So...
sub econst {
my($name, $value) = @_;
my $caller = caller;
*{$name} = sub () { $value };
push @{$caller.'::EXPORT'}, $name;
}
--
Michael G Schwern schwern at pobox.com http://www.pobox.com/~schwern/
It's Flypaper Licking time!
More information about the Belfast-pm
mailing list