in reply to Scope Between global and my
# 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Scope Between global and my
by SkipHuffman (Monk) on Apr 01, 2004 at 18:00 UTC | |
by davido (Cardinal) on Apr 01, 2004 at 18:11 UTC | |
by SkipHuffman (Monk) on Apr 01, 2004 at 18:25 UTC | |
by davido (Cardinal) on Apr 01, 2004 at 18:28 UTC | |
by jdporter (Paladin) on Apr 01, 2004 at 18:31 UTC | |
by SkipHuffman (Monk) on Apr 01, 2004 at 18:40 UTC | |
|