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

Monastic Wizards,

In the code below, open happens or die

open(LOK, ">>C:\\Perl\\RTEOlock\\check\\RTEOlock.log") || die $! ;

Can one or(||) to a subroutine instead die?

If yes, how is it worded?

R_D

20040115 Edit by BazB: Changed title from 'Novice Question #1,000,000'

Replies are listed 'Best First'.
Re: Calling a custom sub on failure
by pzbagel (Chaplain) on Jan 14, 2004 at 21:06 UTC

    You can just replace die with the name of the sub you want to run. You can put practically any command you want after the ||. There is nothing intrinsically special about the open||die pairing, it's just a common convention used to handle errors.

    sub dontdie() { print "I didn't die\n"; } open(LOK, ">>C:\\Perl\\RTEOlock\\check\\RTEOlock.log") || dontdie();
Re: Calling a custom sub on failure
by borisz (Canon) on Jan 14, 2004 at 21:09 UTC
    sure, yes you can.
    open(LOK, ">>C:\\Perl\\RTEOlock\\check\\RTEOlock.log") || this_is_sub( +) ;
    or
    unless ( open(LOK, ">>C:\\Perl\\RTEOlock\\check\\RTEOlock.log") ) { # do what ever we can not open the file ... }
    Boris
Re: Calling a custom sub on failure
by b10m (Vicar) on Jan 14, 2004 at 21:11 UTC

    This can be done several ways (as usual). For example:

    open(LOK, ">>/non-existing/file") || my_subroutine($!); sub my_subroutine { print "Oh no! Error: ".shift; }
    --
    b10m
      Thank you for your helpful response. R_D
Re: Calling a custom sub on failure
by Abigail-II (Bishop) on Jan 14, 2004 at 21:18 UTC
    Can one or(||) to a subroutine instead die?
    Why ask? Why not try yourself?

    Abigail

Re: Calling a custom sub on failure
by mjeaton (Hermit) on Jan 14, 2004 at 20:57 UTC
    I would probably do something like this:

    if (!open($HANDLE, ">>filename.log")) { # call a sub-routine }
    mike
Re: Calling a custom sub on failure
by revdiablo (Prior) on Jan 14, 2004 at 22:20 UTC

    First, it's usually a good idea to use the lower-precedent version of || in instances like this. It is spelled or. This allows you (or perhaps some poor maintenance programmer) to remove the ()'s around open's arguments without making bad things happen.

    Second, what did you try? What's the obvious and simple answer? It's quite easy:

        open LOK, ">>C:\\Perl\\RTEOlock\\check\\RTEOlock.log" or my_sub($!);

    Update: I guess I should refresh my browser tabs more often. I'm not quite sure how I managed to reply an hour later without noticing other replies saying the same thing. :)

Re: Calling a custom sub on failure
by Roy Johnson (Monsignor) on Jan 14, 2004 at 21:30 UTC
    You can (and should) use forward slashes for path components in Perl.

    There's nothing special about die being the right operand of ||. You can use any expression there; it will be evaluated in scalar context. Personally, I prefer the spelled out, low-precedence or for things like this.

    Congratulations on being our millionth customer. Due to our computery nature, though, you don't get any special recognition. We're reserving that for our 1024*1024th customer.

    The PerlMonk tr/// Advocate