Re: Help with syntax local (*in);

2005-12-03 Thread Randal L. Schwartz
 Timothy == Timothy Johnson [EMAIL PROTECTED] writes:

Timothy You're creating a typeglob *in and declaring it to be local.  I'm not
Timothy sure why you would want to do this, but this makes $in, @in, and %in 
all
Timothy local.

and in, and the filehandle/directoryhandle in, also local.  And
the format called in.

Globs are used for a lot of things!

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
merlyn@stonehenge.com URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

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




RE: Help with syntax local (*in);

2005-12-02 Thread Timothy Johnson

You're creating a typeglob *in and declaring it to be local.  I'm not
sure why you would want to do this, but this makes $in, @in, and %in all
local.

-Original Message-
From: Buehler, Bob [mailto:[EMAIL PROTECTED] 
Sent: Friday, December 02, 2005 3:13 PM
To: beginners@perl.org
Subject: Help with syntax local (*in);

local(*in);

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





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




Re: Help with syntax local (*in);

2005-12-02 Thread Shawn Corey

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.0002 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