也许您正在寻找的东西可以通过下面的代码片段来演示
checkpoint
方法
stage
和
data_set
如果代码在执行过程中被中断,则后续运行将在完成后继续启动。
阶段
数据集
.
exit
之后
store_checkpoint($data)
在…的结尾
STAGE2
然后再次运行脚本。
use strict;
use warnings;
use feature 'say';
use YAML;
use Data::Dumper;
my $checkpoint = 'checkpoint.dat';
my $data;
restore_checkpoint() if -e $checkpoint;
STAGE1:
$data->{data_set} = [ title => 'scientist', age => 27, address => '123 street, NY 12345 USA' ];
$data->{next_stage} = 'STAGE2';
store_checkpoint($data);
STAGE2:
$data->{data_set} = [ title => 'professor', age => 45, address => '234 street, WA 98230 USA' ];
$data->{next_stage} = 'STAGE3';
store_checkpoint($data);
STAGE3:
$data->{data_set} = [ title => 'doctor', age => 53, address => '345 street, OK 56789 USA' ];
$data->{next_stage} = 'STAGE4';
store_checkpoint($data);
STAGE4:
$data->{data_set} = [ title => 'CEO', age => 38, address => '456 street, MA 54321 USA' ];
$data->{next_stage} = 'STAGE5';
store_checkpoint($data);
STAGE5:
say 'Done';
sub store_checkpoint {
my $data = shift;
say 'INFO: save_checkpoint ' . $data->{next_stage};
open my $fh, '>', $checkpoint
or die "Couldn't open $checkpoint";
say $fh Dump($data);
close $fh;
}
sub restore_checkpoint {
say 'INFO: restore_checkpoint';
open my $fh, '<', $checkpoint
or die "Couldn't open $checkpoint";
my $yaml = do { local $/; <$fh> };
close $fh;
$data = Load($yaml);
say Dumper($data);
say 'INFO: continue stage ' . $data->{next_stage};
goto $data->{next_stage};
}