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

In reply to Re: Variables in a single file by leocharre
in thread Variables in a single file by northwestdev

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.