
Kanji
User
/ Moderator
Jul 3, 2000, 8:18 PM
Post #2 of 3
(622 views)
|
Re: Recursion and "local" variables.
[In reply to]
|
Can't Post
|
|
Define your hash and the sub definition in the same block so they share the same scope. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> #!/usr/bin/perl -w my $data = 0; recurse( $data ); { my %hash; sub recurse { my $data = shift; $data++; print "%hash contains ", ( %hash ? join( ", " => keys %hash ) : "nothing" ), "\n"; $hash{ $data } = 1; recurse( $data ) unless keys %hash == 10; } } __END__ %hash contains nothing %hash contains 1 %hash contains 1, 2 %hash contains 1, 2, 3 %hash contains 1, 2, 3, 4 %hash contains 1, 2, 3, 4, 5 %hash contains 1, 2, 3, 4, 5, 6 %hash contains 7, 1, 2, 3, 4, 5, 6 %hash contains 1, 2, 3, 4, 5, 6, 7, 8 %hash contains 1, 2, 3, 4, 5, 6, 7, 8, 9</pre><HR></BLOCKQUOTE> There is a proper name for this, but I forget offhand. (Closure?)
|