"Beau E. Cox" wrote:
>
> Hi -
Hello,
> 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?
perldoc perldiag
[snip]
Variable ""%s"" will not stay shared
(W closure) An inner (nested) named subroutine is ref
erencing a lexical variable defined in an outer sub
routine.
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 sub
routines 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 subroutines 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 auto
matically rebound to the current values of such vari
ables.
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>