Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Moving variables to config file

by monk2b (Pilgrim)
on Dec 15, 2007 at 04:02 UTC ( #657159=perlquestion: print w/replies, xml ) Need Help??

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

I would like to use perl code in my config file. I am not able to make this work when using strict, warnings and declaring variables.

Can someone look at my config file and my script and tell me what I need to do to get this to work properly

Script
use warnings; use strict; my ($scalarone,$scalartwo,$scalarthree,$scalarfour,$scalarfive,@arrayo +ne,@arraytwo,%hashone); use lib("/tmp"); do "config.cfg"; print << "PRINT"; ######################################## Scalarone is $scalarone ######################################## Scalartwo is $scalartwo ######################################## Scalarthree is $scalarthree ######################################## Scalarfour is $scalarfour ######################################## scalarfive is $scalarfive ######################################## Arrayone is @arrayone ######################################## Arraytwo is @arraytwo ######################################## Hashone one is $hashone{ONE} Hashone two is $hashone{TWO} Hashone three is $hashone{THREE} Hashone four is $hashone{FOUR} Hashone five is $hashone{FIVE} ######################################## PRINT

Config file
# Scalar Variables $scalarone = "one"; $scalartwo = "two"; $scalarthree = "three"; $scalarfour = "four"; $scalarfive = "five"; # Array Variables @arrayone = ('one', 'two', 'three', 'four', 'five'); @arraytwo = ('six', 'seven', 'eight', 'nine', 'ten'); # Hash Variables %hashone = ( ONE => 'one', TWO => 'two', THREE => 'three', FOUR => 'four', FIVE => 'five');

Thanks for your help

I got 99 problems, but a @%$()_ ain't one.

Replies are listed 'Best First'.
Re: Moving variables to config file
by NetWallah (Canon) on Dec 15, 2007 at 04:31 UTC
    Change my to our and it works.

    Paraphrasing "perldoc -f my",
    the variable is local to the enclosing block, file, or "eval".

    From perldoc -f our

    our EXPR our TYPE EXPR our EXPR : ATTRS our TYPE EXPR : ATTRS "our" associates a simple name with a package variable in +the current package for use within the current scope. When "us +e strict 'vars'" is in effect, "our" lets you use declared g +lobal variables without qualifying them with package names, with +in the lexical scope of the "our" declaration. In this way "our" differs from "use vars", which is package scoped.
    More discussion at this node.

    Update:I would strongly recommend using poolpi's(++) method, below.

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Moving variables to config file
by poolpi (Hermit) on Dec 15, 2007 at 10:13 UTC
    You could have a look at Config-Simple
    It uses less memory than YAML for simple use

    HTH,
    PooLpi
Re: Moving variables to config file
by CountZero (Bishop) on Dec 15, 2007 at 09:23 UTC
    You understand that doing a simple do on a config-file could lead to big security holes? In essence any Perl code can get executed through this mechanism and therefore also any system command, such as to erase everything on your entire stack of hard drives, back-ups included?

    If you just want to save and load variable data to an from a file, have a look at any serialization module, with perhaps YAML and JSON as being the best choice. YAML is the most secure as it never evals your config.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Moving variables to config file
by tuxz0r (Pilgrim) on Dec 15, 2007 at 17:21 UTC
    I agree with the concensus about using Config::Simple, that will suit most of your needs. But, if you do go with the 'our' solution, you might find it handy to put those variables in their own package. Like,
    package MyConfig; # Scalar Variables our $scalarone = "one"; our $scalartwo = "two"; our $scalarthree = "three"; our $scalarfour = "four"; our $scalarfive = "five"; # Array Variables our @arrayone = ('one', 'two', 'three', 'four', 'five'); our @arraytwo = ('six', 'seven', 'eight', 'nine', 'ten'); # Hash Variables our %hashone = ( ONE => 'one', TWO => 'two', THREE => 'three', FOUR => 'four', FIVE => 'five');
    Then, you would refer to %MyConfig::hashone, $MyConfig::scalarone and so forth. Which would keep the variables out of the global name space and give you a visual clue (the package name) that these variables are in the config file. But, again, Config::Simple is by far a better solution.

    ---
    s;;:<).>|\;\;_>?\\^0<|=!]=,|{\$/.'>|<?.|/"&?=#!>%\$|#/\$%{};;y;,'} -/:-@[-`{-};,'}`-{/" -;;s;;$_;see;
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-29 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?