> Hi -
> 
> This sample looks funny ( please don't say 'why would
> you ever want to do that?' ) but it is part of a much
> larger project. This script:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> _main();
> 
> sub _main
> {
>     my $arg1 = shift @ARGV;
>     my $arg2 = shift @ARGV;
> 
>     show_results();
> 
>     sub show_results
>     {
>         print "$arg1 and $arg2\n"; # <- line 17
>     }
> }
> 
> gives the following warnings:
> 
> [EMAIL PROTECTED]:~/src/bempl/junk$ perl ev2.pl mary jane
> Variable "$arg1" will not stay shared at ev2.pl line 17.
> Variable "$arg2" will not stay shared at ev2.pl line 17.
> mary and jane
> 
> Can you nest subroutines? What do the warnings mean?
> 
> Aloha => Beau;
> 

Generally when I hit an unrecognized warning it is time to check the
perldiag docs,

perldoc perldiag

Conveniently,

"Variable "%s" will not stay shared
(W closure) An inner (nested) named subroutine is referencing a lexical
variable defined in an outer subroutine. 

When the inner subroutine is called, it will probably see the value of
the outer subroutine's variable as it was before and during the *first*
call to the outer subroutine; in this case, after the first call to the
outer subroutine is complete, the inner and outer subroutines will no
longer share a common value for the variable.  In other words, the
variable will no longer be shared.

Furthermore, if the outer subroutine is anonymous and references a
lexical variable outside itself, then the outer and inner subrou       
   tines will never share the given variable. This problem can usually
be solved by making the inner subroutine anonymous, using the "sub {}"
syntax.  When inner anonymous subs that reference variables in outer
subroutines are called or referenced, they are automatically rebound to
the current values of such variables."

HTH,

http://danconia.org

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