It sounds as if you want to do something like this:

# First, open a lexical block to hold the configuration variables. { # Now, open a configuration file to pull in data. my %settings; open my $config, "<configfile.dat" or die "Can't get started.\n$!"; while ( my $entry = <$config> ) { my ( $key, $value ) = split /\s+=\s+/, $entry; chomp $value; $settings{$key} = $value; } close $config; # Now, define the sub that will be the "getter" of # the lexical variables. sub mysub { print "Configuration file settings:\n"; foreach my $key ( %settings ) { print "$key = $settings{$key}\n"; } } # Next, close the lexical block. Only things within this block # will have access to the varibles declared with 'my' within it. } # This is the main program block: mysub();

The sub "mysub()" will have access to the lexical variables within the outter block, but since the block terminates before you get to the main part of the program, nobody else will have access to them. This is the first step in building a closure, for example.


Dave


In reply to Re: Scope Between global and my by davido
in thread Scope Between global and my by SkipHuffman

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.