>>>>> "SB" == Steve Bertrand <[email protected]> writes:
SB> My base class reads in a config, and does the following.
SB> BEGIN {
SB> # global variables
those are lexicals scoped to the begin block.
SB> my @global_vars = qw (
SB> GLOBAL_STACK_TRACE
SB> PROFILING
SB> CODE_PATHS
SB> CONFIG_DIR
SB> ACCT_APP
SB> DB_SOURCE
SB> DB_USER
SB> DB_PASS
SB> );
SB> for my $member (@global_vars) {
SB> no strict 'refs';
SB> *{$member} = sub {
SB> my $self = shift;
SB> return $self->{config}{$member};
SB> }
SB> }
SB> }
that can all be done automatically with the Exporter and constant
pragma. why roll your own? it can create those constants and then export
them as real constants (you export slower subs).
SB> If I understand correctly, even though Perl is interpreted, the
SB> compilation only happens once (until the object goes away), so I'd like
SB> to keep this type of functionality within BEGIN blocks.
the begin block saves you nothing there. when that module is loaded it
is compiled and base line code is run. you should be able to strip the
BEGIN and not see a lick of difference. but my solution is still better,
faster and cleaner.
SB> Well, enough with the description..what I want to know is whether the
SB> code I have above that modifies the symbol table could be classified as
SB> a compile-time symbol table dispatch table? Is doing it this way
SB> reliable? Any issues I may run into? I like this, because I wouldn't
SB> have to pass around the dispatch table...it would be inherent.
you shouldn't be munging the symbol table directly anyhow. let modules
that are known to work do the work for you. this is (untested) code that
should do what you want:
use base Exporter ;
BEGIN {
# this needs to be in the begin block so the assignment is done before
# use constant is executed
# i use the hash so you don't need to be explicit with the keys/values
# in two places
my %constants = (
FOO => 1,
BAR => 2,
) ;
use constant %constants ;
}
our @EXPORT = keys %constants ;
uri
--
Uri Guttman ------ [email protected] -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/