Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Keep It Simple, Stupid
 
PerlMonks  

Initializing a script from an INI-file using BEGIN-block

by martinvi (Monk)
on Mar 01, 2005 at 05:24 UTC ( [id://435348]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

martinvi has asked for the wisdom of the Perl Monks concerning the following question:

I've a lot of small scripts working with databases. I like to initialize those scripts from an INI-file using the BEGIN-block of my scripts:

BEGIN { my ( $IniFile, $SectionName, $IniConfig, @ParamNames, $ParamName, $IniValue ); $IniFile = "RefPin.ini"; $SectionName = "DbAttr"; $IniConfig = new Config::IniFiles ( -file => "$IniFile", -default => "$SectionName" ); @ParamNames = $IniConfig->Parameters($SectionName); foreach $ParamName (@ParamNames) { $IniValue = $IniConfig->val($SectionName, $ParamName); $DbAttr{$ParamName} = $IniValue; } }

That code work, so my quest for knowledge involves just the question: Smart? Silly? Outright braindead?

Replies are listed 'Best First'.
Re: Initializing a script from an INI-file using BEGIN-block
by Taulmarill (Deacon) on Mar 01, 2005 at 05:52 UTC
    i wouldn't use a BEGIN block, since it's a lexical space and variables declares with my are not visible from your main code. but besides that i think this is quite sane.
Re: Initializing a script from an INI-file using BEGIN-block
by halley (Prior) on Mar 01, 2005 at 10:17 UTC
    Sharing code between projects is always good. Cut and paste is not a good way to share code, though.

    perlmod gives advice on writing modules.

    If this code were executed by the sub import of a module MyInit.pm which you wrote, then the following code would be all that each little script would need.

    use MyInit;

    --
    [ e d @ h a l l e y . c c ]

      I want to thank you and Taulmarill. This was a very good advice.

      I've put the code into a module -- even an object-oriented one ;-) -- and it works very fine.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://435348]
Approved by Taulmarill
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.