Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Carping in DESTROY.

by eff_i_g (Curate)
on Oct 24, 2006 at 16:21 UTC ( [id://580314]=perlquestion: print w/replies, xml ) Need Help??

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

Monks,

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 (Cardinal) 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";

    Dave

      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

      davido,

      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://580314]
Approved by friedo
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found