http://www.perlmonks.org?node_id=680735

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: die not exiting Application
by Corion (Patriarch) on Apr 16, 2008 at 06:35 UTC

    Most likely you are not using strict. Your line makes little sense because open usually takes at least two parameters, the filehandle to be opened and the name of the resource to be accessed:

    open(my $fh, "<", $file) or die("file open failed for '$file': $!");

    That said, your code fails for me as expected:

    X:\>perl -e "open($file) || die('file open failed');" file open failed at -e line 1. X:\>perl -e "open('xx') || die('file open failed');" file open failed at -e line 1.

    I didn't know and haven't used the 1-argument form of open and for good reason, because that means action at a very large distance:

    perl -e "$FILE='file_that_exists';open(FILE) || die('file open failed' +);"

    In this case, the 1-argument form does not die because the file named in $FILE exists. But that is substantially different from your code.

    Update You silently update your node. I consider that quite rude. Please mark updates to your node with <b>Update:</b> or something and don't overwrite the complete node.

    Your new code fails for me as expected:

    X:\>perl -w tmp.pl Name "main::IN" used only once: possible typo at tmp.pl line 5. Couldn't open `/tmp/test123' at tmp.pl line 5. X:\>type tmp.pl use strict; sub push_report { my $idx = shift; my $file= "/tmp/test123"; open(IN, $file) || die "Couldn't open `$file'"; }

    Maybe you want to do more rigorous checks:

    use strict; sub push_report { my $idx = shift; my $file= "/tmp/test123"; if (-e $file) { warn "'$file' exists"; } else { warn "'$file' does not exist"; }; if (-f $file) { warn "'$file' exists as a file"; }; if (-d $file) { warn "'$file' exists as a directory"; }; open(IN, $file) || die "Couldn't open `$file'"; }
Re: die not exiting Application
by chromatic (Archbishop) on Apr 16, 2008 at 06:36 UTC

    We can only guess until you show a complete example of ten to fifteen lines of code which demonstrates this behavior.

Re: die not exiting Application
by Anonymous Monk on Apr 16, 2008 at 07:37 UTC
    Um, just a wild guess here but is that perhaps all the code? Cos if it is you need to call push_report somewhere. That's the only way i can get that code to not die - by omitting (as you have in the code snippet) the call to push_report
      I have noticed that the above problem is happening when we have TK error along with the open fail error in that case. TK:Error API get called instead of die_handler. Tk error are not considered fatal, so the application don't exit. But, this is not the right behavior of the application. Regards, Mihir

        In case die is overridden, you can call CORE::die instead.

        Update: maybe I should mention that this wouldn't help much, though, if the die should somehow happen to be trapped by an eval { ... } block  (which I don't know)...  In this case, you could try something along the lines of

        open my $fh, "<", $file or warn "Couldn't open '$file': $!" and ex +it;

        to force the program to terminate.

        Consider this

        my $file = "does-not-exist"; while (<STDIN>) { eval { open my $fh, "<", $file or warn "Couldn't open '$file': $!" and exit; }; print STDERR "ERROR: $@" if $@; }

        (which would actually terminate the program) vs.

        while (<STDIN>) { eval { open my $fh, "<", $file or CORE::die "Couldn't open '$file': $!"; }; print STDERR "ERROR: $@" if $@; }

        which would continue printing (despite the CORE::die)

        ERROR: Couldn't open 'does-not-exist': No such file or directory at ./ +680735.pl line 10, <STDIN> line 1. ERROR: Couldn't open 'does-not-exist': No such file or directory at ./ +680735.pl line 10, <STDIN> line 2. ...

        ...if you keep hitting the ENTER key.

        (If necessary, you could of course also use CORE::warn or CORE::exit in combination with the above...)

Re: die not exiting Application
by Anonymous Monk on Apr 16, 2008 at 06:59 UTC
    Not possible
    sub push_report { my $idx = shift; $file= "/tmp/test123"; open(IN, $file) || die "Couldn't open `$file'"; } __END__ Global symbol "$file" requires explicit package name at - line 4. Global symbol "$file" requires explicit package name at - line 5. Global symbol "$file" requires explicit package name at - line 5. Execution of - aborted due to compilation errors.
      Not possible
      It is possible indeed if strict is not used.
      $ perl -v This is perl, v5.10.0 built for i686-linux [...] $ perl sub push_report { my $idx = shift; $file= "/tmp/test123"; open(IN, $file) || die "Couldn't open `$file'"; } __END__ $
      --
      When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do. [1]
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: die not exiting Application
by ahmad (Hermit) on Apr 16, 2008 at 18:14 UTC

    You should call the sub in-order to get it processed

    Something like this should work

    use strict; sub push_report { my $idx = shift; my $file= "/tmp/test123"; open(IN, $file) || die "Couldn't open `$file'"; } push_report();