Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Include external variables?

by johnfl68 (Scribe)
on Dec 03, 2012 at 00:34 UTC ( [id://1006770]=perlquestion: print w/replies, xml ) Need Help??

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

Hello:

Is there a way to easily include external variables?

I have about a half dozen scripts that have a good deal of variables that are the same in all of them. Things like font file locations, look-up tables, etc. It would be nice if I could put these all in a common file, and then just include that file like a module. Things like this and many more:

$font41 = "/usr/share/fonts/liberation-fonts/LiberationSans-Reg +ular.ttf"; $font42 = "/usr/share/fonts/liberation-fonts/LiberationSans-Ita +lic.ttf"; $font43 = "/usr/share/fonts/liberation-fonts/LiberationSans-Bol +d.ttf"; $font44 = "/usr/share/fonts/liberation-fonts/LiberationSans-Bol +dItalic.ttf"; my %w16towdirection = ( "N", "North", "S", "South", "E", "East", "W", "West", "NE", "Northeast", "SE", "Southeast", "SW", "Southwest", "NW", "Northwest", "NNE", "North-northeast", "ENE", "East-northeast", "ESE", "East-southeast", "SSE", "South-southeast", "SSW", "South-southwest", "WSW", "West-southwest", "WNW", "West-northwest", "NNW", "North-northwest", );

I'm sure this can be done, but in searching I think I am not using quite the correct terms to get results that seem to be the answer I think I am looking for.

Any help pointing me in the right direction would be appreciated. Thanks!

Replies are listed 'Best First'.
Re: Include external variables?
by LanX (Saint) on Dec 03, 2012 at 00:51 UTC
    > I am not using quite the correct terms to get results

    it's called a config file.

    There many approaches, including YAML or JSON to avoid code execution during inclusion.

    If there are no security concerns, the simplistic approach is to use do or require to execute a perl-file defining global variables.

    Cheers Rolf

Re: Include external variables?
by BillKSmith (Monsignor) on Dec 03, 2012 at 02:04 UTC

    A true module should do the job for you, but it is generally considered poor practice to export variables. In your case they are used as contants that are widely used in your application. You would not be concerned about "polluting" symbol space or accidentally changing their values. Go for it.

    Bill
Re: Include external variables?
by pushtaev (Sexton) on Dec 03, 2012 at 05:26 UTC

    If you use Moose, take a look at MooseX::SimpleConfig.

    If you don't but still use OOP, take a look at it anyway, just to understand the idea.

    If you don't use OOP at all, I recommend you to create a big and complex hash variable and export it, instead of export all this "$font44".

    Look at http://perldoc.perl.org/Exporter.html to learn about Exporter.

Re: Include external variables?
by Anonymous Monk on Dec 03, 2012 at 07:39 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found