Search code examples
perlcoding-styledata-dumper

Is there a safe way of using eval to unthaw Data::Dumper output in Perl?


I have an object that uses freezes data as such:

sub frozen_data {
    my $self = shift;

    $Data::Dumper::Indent = 0;
    $Data::Dumper::Terse  = 1;
    return Data::Dumper->Dump( [ $self->{_DATA}, ] );
}

and a corresponding thaw:

sub thaw_data {
    my ($self) = @_;

    $self->{_DATA} = eval $self->{DATA};
}

this seems to work well, are there any issues with it? I also tried running this by perlcritic, and I get this:

Expression form of "eval" at line 69, column 22.  See page 161 of PBP.  (Severity: 5)

What's the better way to write this?


Solution

  • Decide what you want to allow or forbid and set up a Safe compartment and use its reval method.