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

I am new to this but would appreciate any help that I could recieve on this. I am writing a sub to store data using storable module. everything works great except for the retrieve part. When calling the retrieve
$existref = retrieve($filename);

I am checking to see if the file exists before calling retrieve but if the the file exists but it can't be retrieved I want to pass back the caller of my function a nice error message "get record failed". right now it just blows the cgi up and says the the file can not be opened.
Help I am frustrated on this simple issue.
pat

Replies are listed 'Best First'.
Re: Storable Question
by chipmunk (Parson) on Jan 31, 2001 at 02:23 UTC
    According to the documentation for Storable:
    Storable uses the "exception" paradigm, in that it does not try to workaround failures: if something bad happens, an exception is generated from the caller's perspective (see Carp and croak()). Use eval {} to trap those exceptions.
    So, your code might look something like this:
    $existref = eval { retrieve($filename) }; if (not $existref) { nice_error_message("Error retrieving data: $@"); }
      I'm sure you here this enough but I will say it anyway.
      Thanks.
      It works perfectly it is exactly what i wanted.. This site is great!
Re: Storable Question
by arturo (Vicar) on Jan 31, 2001 at 02:24 UTC

    Use the -r filetest operator; it will fail if the file does not exist, AND if the file is not readable (you might want to run -e on it if -r fails, just to check which one)

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor