in reply to Re: Preventing multiple instances
in thread Preventing multiple instances

my $highlander; BEGIN { open $highlander, '<', $0 or die "Couldn't open $0: $!\n"; flock $highlander, LOCK_EX | LOCK_NB or do { warn "There can only be one $0\n"; exit; }; }

I am confused by there being a do { warn "XXX"; exit; } in this snippet rather than a simple die "XXX", especially seeing as how you are calling die if the open fails in the preceding statement. Would you be so kind as to explain the reasoning, please?


🦛

Replies are listed 'Best First'.
Re^3: Preventing multiple instances
by davido (Cardinal) on Jun 04, 2021 at 15:44 UTC

    Good question.

    My thought process when I wrote that was that having the process already running wasn't an exceptional event. It might be noteworthy, but not worthy of a non-zero exit code. But that's entirely subjective to the use-case. I had recently been working on something where it was fairly normal behavior for an instance already to be running, and for that to be a perfectly acceptable situation.

    So that's really it. A die would be fine too, but would (on a POSIX system) result in an 11 exit code, resource temporarily unavailable. By separating the message from the termination I went the other direction where the exit code is zero, while STDERR still gets a little quip.


    Dave