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 10: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 15: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.