billyak has asked for the wisdom of the Perl Monks concerning the following question:

I'm sure there is a more accurate name for what I am trying to do, so this title will have to do. More or less, I've got a package that I am storing the state using Data::Dumper and loading it back in with eval. You're probably already starting to cringe ;-)

Here is a short, simplified snippet that displays what I've got so far and what I need to know how to fix. It will probably only be one line. (This is just a brief overview of what I'm trying to do; the real code is running with strict, etc.)
my $test = Test->new(123); $test->change(345); print $test->{id}; # Should print 345 package Test; sub new { bless{ id=>$_[1], } } sub change { my($self,$id) = @_; my $t2 = Test->new($id); $self = $t2; # This line is broke }

Any ideas on how to obtain the desired effect? On a side note, is storing states with Data::Dumper so terrible that I should do something else?

Thanks,
-billyak

Replies are listed 'Best First'.
Re: Object Swaps
by japhy (Canon) on Jan 14, 2002 at 04:31 UTC
    You'll want to assign directly to $_[0].
    sub change { my ($self, $id) = @_; $_[0] = Test->new($id); }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Object Swaps
by gav^ (Curate) on Jan 14, 2002 at 04:50 UTC
    Personally I prefer using Storable rather than Data::Dumper/Eval. It is both faster and safer.

    Some good information about Perl OO Persistence can be found here.

    gav^