Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
using the canonical "power of 2" example, move the inner routine to the end of the outer routine.
use strict; use warnings; sub print_power_of_2 { my $x = shift; my $result = power_of_2(); print "$x^2 = $result\n"; sub power_of_2 { return $x ** 2; } } print_power_of_2(5); print_power_of_2(6);
this results in the "variable not shared" run-time warning and perlcritic errors with "inner sub from being global".
if i MUST keep the location of the inner routine; then i can't do the "convert to anonymous routine" remedy.
sub print_power_of_2 { my $x = shift; my $result = power_of_2(); print "$x^2 = $result\n"; my $power_of_2 = sub { return $x ** 2; }; }
because the routine is declared after its invocation.
what alternatives do i have? thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: nested routines, anonymous routines and location dependency
by Joost (Canon) on Mar 04, 2008 at 22:59 UTC | |
by Anonymous Monk on Mar 04, 2008 at 23:08 UTC | |
by Joost (Canon) on Mar 04, 2008 at 23:18 UTC | |
by Anonymous Monk on Mar 04, 2008 at 23:25 UTC | |
by chromatic (Archbishop) on Mar 04, 2008 at 23:40 UTC | |
| |
by ikegami (Patriarch) on Mar 04, 2008 at 23:42 UTC | |
by ysth (Canon) on Mar 05, 2008 at 02:32 UTC |