Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Carping in DESTROY.

by eff_i_g (Curate)
on Oct 24, 2006 at 16:21 UTC ( #580314=perlquestion: print w/replies, xml ) Need Help??
eff_i_g has asked for the wisdom of the Perl Monks concerning the following question:


I'm calling Storable's store within a DESTROY method, and I'm not getting an error for a read-only file. I was (still am) very puzzled, but I eventually received the error by changing "DESTROY" to "end" and explicitly calling $object->end();.

Why am I not getting the error in DESTROY, and how can I handle this properly? A tiny code snippet is below, which works properly when DATA_FILE is writeable:
sub DESTROY { my $self = shift; store $self, DATA_FILE; }
I appreciate the help as I venture further into OOP :)

Replies are listed 'Best First'.
Re: Carping in DESTROY.
by davido (Archbishop) on Oct 24, 2006 at 17:18 UTC

    Storable's documentation states that store() returns undef on failure, and that failure usually indicates an IO error. Thus, you should be checking its return value just as you would check the return value of other IO calls.

    store $self, DATA_FILE or die "Couldn't store object.\n";


      I don't know where this is documented but die() in DESTROY isn't thrown on my 5.8.7.

      sub A::DESTROY { die 'ok' } my $obj = bless [], 'A'; $obj = ''; # no exception occurs print "$@\n"; # " (in cleanup) ok at - line 1."

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        It does get thrown — anything after the die won't get executed — but it's intercepted by DESTROY's caller.

        my $var = 1; sub A::DESTROY { $var = 2; die 'ok'; $var = 3; } my $obj = bless [], 'A'; $obj = ''; # no exception occurs print "$var\n"; # 2 print "$@\n"; # " (in cleanup) ok at - line 1."

        This behaviour isn't new. Tested with 5.6.1


      I tried die and eval; neither of them show an error. I'll prepare some example code...

Re: Carping in DESTROY.
by madbombX (Hermit) on Oct 24, 2006 at 17:36 UTC
    You should also be storing the object and not a reference to the object:
    store \$self, DATA_FILE or warn "store error (%a): $!\n";

    You should also always be in the habit of checking your results (hence the or warn "blah";).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://580314]
Approved by friedo
Front-paged by diotalevi
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-06-23 20:10 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.