Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: mod perl global initialization

by Tomte (Priest)
on May 11, 2004 at 09:00 UTC ( [id://352354]=note: print w/replies, xml ) Need Help??


in reply to mod perl global initialization

As the static globals aren't a concern, I'll concentrate on a proposal for your INIT.pl

I wouldn't use INIT.pl as global-variables repository. Instead I would define and export a sub in it, that'd assamble the dynamic init-stuff into a hash and return this. Instead of using

INIT::thisvar; INIT::thatvar
you had to use
INIT::getInitvars()->{thisvar}; INIT::getInitvars()->{thatvar};
or, better yet
my $initHash = INIT::getInitvars() $initHash->{thisvar}; $initHash->{thatvar};
the Module could look like this (not working code, just a pseudo-perl example out of nothing):
package INIT; use Exporter; use vars qw( %static_1 %static_2 ); @ISA = qw(Exporter); @EXPORT = qw(&getInitvars); @EXPORT_OK = qw(%static_1 %static_2); %static_1 = ( this => "that", ); %static_2 = ( that => "this", ); sub getInitvars { my $result = {}; # push the static stuff push @$result, %static_1; push @$result, %static_2; # create the dynamic stuff push @result, _dynamicStuff(); $result; } sub _dynamicStuff { # compute stuff and return a hash }
Using non-static, not-constant global variables is likely to create a maintenance nightmare; avoid them where possible; using a solution as the proposed isn't much more work, but easily expanded if your needs change.

Edit: declared the static hashes, looks betterthat way;-)

regards,
tomte


An intellectual is someone whose mind watches itself.
-- Albert Camus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-23 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found