in reply to Re^7: Help! My variables are jumping off a cliff!
in thread Help! My variables are jumping off a cliff!
dave_the_m's example leads to a very practical, if slightly contrived use for double declaration.
You have a sub that returns two subrefs. Those two subrefs are, as you might assume from their being instantiated at the same time, related. And they both rely upon a variable of some domain specific name that they inherit through closure. But they each need a different one:
sub generator { ... my $frobnosticator = 0; my $coderef1 = sub{ ...; $frobnosticator ^= 1; ... }; my $frobnosticator = 0; my $coderef1 = sub{ ...; $frobnosticator ^= 1; ... }; return $coderef1, $coderef2; }
There, perfectly sensible :)
|
|---|