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

I'm seeing in STDERR the following:
Uncaught exception from user code: Can't open('LOC_TMPFILE', 'C:\UBackupTemp\zzip_backuptmp.zip'): No + such file or directory at \\martins\SAMSUNG-HD103SI-01\Backup\UBacku +p\ubackup.pl line 418 at (eval 38) line 74 main::__ANON__('LOC_TMPFILE', 'C:\UBackupTemp\zzip_backuptmp.zip') + called at \\martins\SAMSUNG-HD103SI-01\Backup\UBackup\ubackup.pl lin +e 418
The code itself (line 418) is:
# Open our temp zip file open(LOC_TMPFILE, BACKUP_LOCAL_DIR().BACKUP_TMP_ZIP()) or die("Error reading file, stopped: ".$!);
It doesn't look like the exception handling is working! Any help as to why would be gratefully received??

Replies are listed 'Best First'.
Re: open or die..
by Anonymous Monk on Feb 14, 2015 at 20:48 UTC

    open doesn't normally die on errors, so actually, your exception handling kind of is working ;-)

    Looks to me like you might be using a combination of diagnostics and autodie:

    #!/usr/bin/env perl use warnings; use strict; use diagnostics -traceonly; use autodie; open FOO, '<', '/tmp/foo/' or die "ACK! $!"; __END__ Uncaught exception from user code: Can't open '/tmp/foo/' for reading: 'No such file or directory' at - + line 6 main::__ANON__("FOO", "<", "/tmp/foo/") called at - line 6

    If you don't want open to die and you want to handle the error yourself, you can lexically disable autodie, as shown in its synopsis:

    { no autodie qw(open); # open failures won't die open(my $fh, "<", $filename); # Could fail silently! # ^^^ add your "or die" to this open }
      Many Thanks for this! It IS indeed Autodie! Your wisdom is much appreciated, I missed this!
Re: open or die..
by Corion (Patriarch) on Feb 14, 2015 at 19:43 UTC

    If you think your exception handling is not working, why do you show us the code where the exception gets thrown?

    Maybe you want to show us the code of your exception handling and tell us how it fails to do what you think it should do?

    Have you tried replicating the situation without needing a file? Does your exception handling work for a simple, unconditional die statement?

      Yes, an exception is raised, but why doesn't it print the message that is given as a param to the die built-in? I would expect that.

      For example:

      $ perl -e 'open my $FOO, "<", "foo2.txt" or die "Could not open file + foo2.txt : $!";' Could not open file foo2.txt : No such file or directory at -e line 1.

      Je suis Charlie.

        I completely missed that the error message as caught was not the error message as (should have been) raised.