$attached = attach_var($target_ref, $attach, [$optional_name]); $attached = attached($target_ref, [ $optional_name ]); $attached = unattach_var($target_ref, [ $optional_name ]); # A few trivial examples. sub elements(\@$){ my ($ref,$size) = @_; $size = 1 unless $size; my $info = attached($ref,'elements') || attach_var($ref,[0,$size],'elements'); my @ret = (); if( $info->[0] >= @$ref ) { unattach_var($ref,'elements'); return; }else{ @ret = @{$ref}[$info->[0]..($info->[0]+$info->[1] -1)]; $info->[0]+= $info->[1]; } @ret; } while( my @els = elements(@large_array,4) ){ # Process non destructively } # Lazily generate a span of integers. sub int_span($$){ my $info = attached(\$_[0],'INT_SPAN') || attach_var(\$_[0],[$_[0],$_[1] ],'INT_SPAN'); unattach_var(\$_[0],'INT_SPAN') and return if $info->[0] > $info->[1]; return $info->[0]++; } while(my $i = int_span(1,1000000000)){ print $i,"\n"; } #### # Persistant subroutine data sub somesub { my $p_args = attached(\&somesub,'somesub') || attach_var(\&somesub,{called=>0},'somesub'); ++$p_args->{called}; # The persistant arg hash could be keyed on the caller info if desired # for context sensitivity }