in reply to how can i hold temporary values in a recursive function
Several ways:
{ my %state; sub recursive { my $arg = shift; ... if( ( $arg = $state{ $arg } ) == ... ) { return recursive( $arg ); else { return 0 } } }
sub rec_helper { my %state; my $recursive; $recursive = sub { my( $arg ) = shift; ... if( ( $arg = $state{ $arg ) ) == ... ) { return $recursive->( $arg ); } else { return 0; } }; }
sub recursive { my( $state, $arg ) = @_; ... if( ( $arg = $state->{ $arg } ) == ... ) { return recursive( $state, $arg ); } else { return 0; } } sub rec_helper{ my $arg = shift; my %state; return recursive( \%state, $arg ); }
And many variations on those themes.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how can i hold temporary values in a recursive function
by ikegami (Patriarch) on Apr 18, 2010 at 21:04 UTC |