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


In reply to Callstack manipulation? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.