You're going to have to make sure that your program is aware of what it is doing, and that it can pick up where it left off. This is not necessarily trivial, especially if the work each function does is not entirely trackable.

Maybe this will do something useful:
#!/usr/bin/perl -w use strict; use constant RDELIM => "\x00"; use constant ADELIM => "\x01"; my @state; my @f_state; my $state_file = "saved.state"; $SIG{INT} = \&freeze; sub freeze { open (STATE, ">$state_file") || die "Could not save state to $state_file, sorry\n"; foreach (@state) { my ($args, $vars) = @$_; print "@$args...@$vars\n"; print STATE join(RDELIM, @$args); print STATE ADELIM; print STATE join(RDELIM, map { $$_ } @$vars); print STATE "\n"; } close (STATE); exit (0); } sub thaw { # If the file can't be opened, just restart open (STATE, $state_file) || return; @f_state = (); while (<STATE>) { chomp; push (@f_state, [ map { [ split(RDELIM, $_) ] } split(ADELIM, $_) ]); } close (STATE); } sub arg_push { push(@state, [shift(@_), [@_]]); } sub arg_pop { pop(@state); } sub arg_thaw { my ($args,$vars) = @{shift(@f_state)}; @{shift()} = @$args; foreach (@_) { $$_ = shift(@$vars); } } use constant DEPTH => 4; sub recurse { my ($x,$y,$X,$Y); my @args = @_; arg_push(\@args, \$x, \$y); # If there is frozen data specified to at least # this depth... if (@f_state) { # ...use that instead. arg_thaw(\@args, \$X, \$Y); print "Resuming @args ($X,$Y)\n"; } my ($what) = @args; $X = 0 unless (defined($X)); $Y = 'a' unless (defined($Y)); print "$what\n" if (@state == 3); if (length($what) <= DEPTH) { for my $x_iter ($X..'9') { for my $y_iter ($Y..'z') { $x = $x_iter; # Copy $y = $y_iter; recurse("$what$x_iter$y_iter"); } } } pop(@state); } thaw(); recurse('');

In reply to Re: Saving the state of a recurseive program by tadman
in thread Saving the state of a recursive program by Tardis

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.