{ my $list_ref=[]; sub gather (&){ my $code_ref=shift; my @list=(); my $safe=$list_ref; $list_ref=\@list; &$code_ref; $list_ref=$safe; return @list; } sub take { push @$list_ref, @_; return; } } @x= gather { for $c (1..10){ for $b (1..$c){ for $a (1..$b){ if ( $c**2 == $b**2 + $a**2){ take [$c,$b,$a] } } } } }; }