use strict; package Persistent::Code; sub new { my ($class,$src,$permflag) = @_; my %flags; $flags{'perm'} = 1 if $permflag; my $cr = eval "sub { $src }"; return undef if $@; my $self = [$cr,$src,\%flags]; bless $self, $class; } sub src { my $self = shift; $self->[1]; } sub coderef { my $self = shift; $self->[0]; } sub debug { my $self = shift; @_ ? ($self->[2]{debug} = shift) : $self->[2]{debug}; } sub perm { my $self = shift; @_ ? ($self->[2]{perm} = shift) : $self->[2]{perm}; } sub run { my $self = shift; my $pkg = ref $self; if ($self->[2]{debug}) { my $src = $self->[1]; print "eval: ",$src; wantarray ? eval "(&sub {$src}(@_))" : eval "&sub {$src}(@_)"; } else { goto &{$self->[0]}; } } sub STORABLE_freeze { my ($self,$cloning) = @_; $self->[2]{perm} ? $self->[1] : ''; } sub STORABLE_thaw { my ($obj,$cloning,$code) = @_; return if !$code; my $class = ref $obj; my $newobj = $class->new($code,'perm'); @$obj = @$newobj; } 1;