On Fri, Dec 15, 2006 at 10:51:09AM +0000, Jorge Almeida wrote:
> I thought I already knew a few things about Perl, but I just found out I
> don't:
>       #!/usr/bin/perl -w
>       use strict;
>       use diagnostics;
>       my $a="27";
>       doit();
>       sub doit{
>               print "$a\n";
>       }
> 
> The output of this program is:
> 27
> 
> Just what I would expect if line 4 had "our" instead of "my".
> What am I missing?
> TIA.

Simply put:

my() is used to declare lexical variables.  These variables apply in
their current scope, including nested scopes.  They do not apply in
enclosing scopes.

This occasionally seems confusing to people coming from languages that
treat certain types of local (but not lexical) variables as "lexical",
even though they technically aren't.  Python comes to mind, with
variable scope that is sometimes defined as lexical by default even
though technically it's a special case of dynamic local scope that just
usually acts very similarly to lexical scoping.

Hope that helps.

-- 
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
print substr("Just another Perl hacker", 0, -2);

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