I put together an anonymous subroutine which partly
works. There is an array called @tasks which is defined as a
local variable in the main routine and when I call the anonymous
subroutine, one can still read all the elements in @tasks.

        There is also a single scaler called $task, also created
in main and a foreach loop
        
foreach $task (@tasks) {    #may be a lease
    @results = whatisit($task);
    if ( $results[0] eq "IP" ) {    #This could be bootP or dynamic.
        $numerical_lookup = ipaddr_to_num($task);
        @dynamics         = netfind($numerical_lookup);

        #dynamics[1] has a value when this is a dynamic lease.
        if ( $dynamics[1] ) {       #It's dynamic.
&$report_dynamic;
        }         #It's dynamic.
        else {    #static
&$report_static;
        }    #static
    }    #This could be bootP or dynamic.

All the code in my sample that you see does work as it should
and gets you to either the report_dynamic orreport_static
anonymous subs. Here is the beginning of the report_static
subroutine. First I declare a local variable in main to use for
the name of the anonymous subroutine.

my $report_static;

That is fine. The trouble starts immediately when the subroutine
does.

    $report_static = sub {#What is at this static address?
            @hostnames = revdns($task);

That's where things explode. If I trace each step in that
subroutine, I can still read @tasks but $task has disappeared
the instant I am in the report_static subroutine.

        This is a pretty large application, about 1230 lines and
the logic did work when it all was part of main.

        I would at least think I understood what was happening
if both the @tasks array and the $task scaler became undefined
in report_static but only the scaler goes "poof!" What I was
hoping for was for the loop in the main portion to select which
element in @tasks was used and the anonymous subroutine would do
what it needed to do just as it had done when it was all in
main.

        I am familiar with declaring my $variable; or our
$variable; if one shares variables between subroutines but I
just assumed that in an anonymous subroutine, one was still
working with the same scope as the variables in the routine that
called the subroutine.

        Thanks for any constructive suggestions.

Martin McCormick

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to