Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Variables in a single file

by leocharre (Priest)
on Sep 16, 2009 at 16:00 UTC ( [id://795654]=note: print w/replies, xml ) Need Help??


in reply to Variables in a single file

Well, what is your code? Is it a script only? Then here's one example..
#!/usr/bin/perl use strict; use vars qw/$NAME $AGE $DATE/; $NAME = 'jim'; $AGE = 29; $DATE = `date`;

In that example, the variables reside in namespace main::, so that from anywhere in the program, $main::AGE will hit 29 in this example.

If this were a module..

package Sunflower; use strict; use vars qw/$NAME $AGE $DATE/; $NAME = 'jim'; $AGE = 29; $DATE = `date`;

Then from anywhere in the code (there are some variations), you can access $Sunflower::AGE.

(You do have to realize that if you code a module (pm), then that example there, those values are set at compile time, before your code calling 'use Sunflower;' runs.. Worry about this later.)

I would say if you expect your values to change from time to time.. How about a YAML config file?

#!/usr/bin/perl use strict; use YAML; my $abs_conf = '/etc/sunflower.conf'; my $config_data = YAML::LoadFile($abs_conf); printf "Age is %s, name is %s, date is %s\n", $config_data->{AGE}, $config_data->{NAME}, `date`;

And in your /etc/sunflower.conf..

---
AGE: 29
NAME: jim

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found