On Mon, 15 May 2000 14:11:16 -0700, "Justin Rogers" <[EMAIL PROTECTED]>
wrote:

>I am under the impression that my is for the following:
>
>
>my $var = 'Whatever';
>
>sub Hey
>{
>  print $var;
>}

>Shouldn't print anything...  my is local scope without recursion.  I know
>that this works for subroutines.  Declaring a variable local makes a copy

This is wrong!  Why don't you try it before posting to the list?  Lexical
variables are visible from within nested scopes!

One thing easily overlooked in the code above is that it actually creates
a closure.  The $var variable will not be destroyed together with all the
other lexicals that have file scope:  The Hey() function has a reference
to $var in its scratchpad.  And Perl doesn't know if Hey() might still get
called during global destruction.  Therefore both Hey() and $var will live
on a little longer.  This effectively gives $var the same behavior (and
problems) as a real global variable.

To prevent this closure effect, pass global lexicals as parameters.  All
this really only makes a difference if you store objects in this variable.
Normal scalar values aren't really destroyed anyways.

>of the variable.  While declaring a variable with $my makes a new variable.
>
>So declaring global variables with my means that subroutines can't change

This isn't the case.  All subroutines defined *after* the lexical variable
can access and modify it, if it is declared at file scope.  Only lexicals
defined in inner scopes are inaccessible from outer scopes:


my $a;
sub myfun {
    my $b;
    # $a and $b are *both* accessible here
}
# only $a is accessible now

>them.  Declaring local means subroutines and any *child* elements of the current
>package can change them.  So there are a few subtle differences.  If someone
>can explain this better then please do.  I'm a perl novice at the most.

-Jan

---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to