LanX has asked for the wisdom of the Perl Monks concerning the following question:
Hi
Following a discussion about how to realize gather/take in perl5 as a lazy iterator (-> see in German perl-community.de) I wonder if it's possible to manipulate the callstack or respectively to switch between different callstacks.
Precisely, is it possible to make a call to a subroutine "take()" after entering act as if I did a "goto &take()" by poping the callinfos from the callstack to a different stack? And sometimes later I wood need to reestablish the callinfo in the stack to jump back to exactly the same position were I called take().
Maybe my English is not clear enough, look at this example:
{
my $j=0;
my $resume=0;
sub gather {
goto $resume if $resume; # Dispatcher
while (++$j<=5) {
$resume="getin", goto getout
unless ( $j % 2 ); # Take
getin:
}
$resume=0; # Final exit
return;
getout:
return $j;
}
}
while ( my $a=gather() ) {
print "<<$a>> "; # prints <<2>> <<4>>
}
would be much nicer to read and maintain if I was able to write kind of
{
my $resume=0;
sub gather {
unless ($resume) { # Dispatcher
push @callstack,$resume;
return;
}
for my $j (1..5) {
take($j) unless ( $j % 2 ); # Take even numbers
}
$resume=0; # Exit
return;
}
sub take {
$resume=shift @callstack;
return @_;
}
}
The "Dispatcher" and "Exit"-chunk of the gather-sub could be automatically added, and this behaviour would circumvent the problem that it's not possible to jump into foreach-loops.
This assembler-technique dates back to my 68000 programming and I'm well aware that the callstack in Perl has much more data then just the returnadress.
So once again: Are there any opcodes allowing manipulating the callstack in this manner? To make a sub-call act like a goto &sub afterwards without weird sideaffects???
This would lead to a general approach to continuations.
Thx
Rolf
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Callstack manipulation?
by plobsing (Friar) on Nov 01, 2008 at 19:19 UTC | |
by LanX (Saint) on Nov 01, 2008 at 20:01 UTC | |
by plobsing (Friar) on Nov 02, 2008 at 04:02 UTC | |
by LanX (Saint) on Nov 02, 2008 at 12:57 UTC | |
by LanX (Saint) on Nov 03, 2008 at 01:53 UTC | |
|
Re: Callstack manipulation?
by LanX (Saint) on Nov 01, 2008 at 21:53 UTC | |
by Anonymous Monk on Nov 21, 2008 at 07:03 UTC | |
by LanX (Saint) on Nov 21, 2008 at 10:11 UTC |