Buehler, Bob wrote:
local(*in);

Does this indicate that you want to make all variables that begin with
$in private?


No.

Perl has two kinds of scoping: lexical and dynamic. Lexical scoping means the name only has meaning with the block, or if outside any block, within the file. Dynamic scoping means within the block and in any subroutine called in the block. The keyword local is used to set dynamic scoping. For example:

open FILE, "< myfile.txt" or die 'cannot open myfile.txt: $!\n";
my $contents = '';
{
  local $/;            # set $/ to undef
  $contents = <FILE>;  # slurps the entire file, \n's included
}
# End of block, $/ reset to whatever it was before.
close FILE;

Here dynamic scoping is used as a stack to preserve $/ while the file is slurped.

In 'local(*in);' the phrase '*in' is a typeglob. It means every identifier 'in'. This includes $in, @in, %in, sub in, and file handler 'in'. Localizing 'in' would set all of them to undef.

To privatize a variable, use 'my'. Try this example:

#!/usr/bin/perl

use strict;
use warnings;

my $var = 'This is a test.';
print "$var\n";

{
  my $var = 'foobar';
  print "$var\n";
}

print "$var\n";

__END__

'my' privatizes $var to the file and to the block while inside the block. You would have to use 'our' (in both files) to get a global variable shared between files.

You will have to experiment with 'my', 'our', and 'local' to see exactly what they do; an full explanation is beyond a simple e-mail.


--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to