Perhaps my real question is: "Is this feature suitable for learning Perl 6"? Based on If this Perl 6 CATCH block can change variables in the lexical domain? It seems that the simplest example may be beyond the scope of a simple example.
In this question, I was working with something that seems dumb or better than the other for this particular problem, because I was playing with this function and not solving the problem.
There, the documented use of warnings in the form of special types of exceptions ("control exceptions") in which you receive a message can catch it if you want, but it can also ignore it and it will resume on its own (although I was rather stupid about it in Where should I catch the Perl 6 control check exception? ).
In addition, I’m thinking about where the caller can handle a failure outside the scope of the called party. For example, reconnecting to the database, fixing missing directories and other problems with external resources for which responsibility is not responsible.
In reading these kinds of things in other languages, the advice was mainly not to use them, because in programming the "real world" people still can not cope with this problem at all.
The answer to repeating the C # exception handler in the following seems to be talking about this bad practice and ugly code. Of course, I did not understand the way to hide a bunch of code in the called one.
I cracked this example, although I'm not sure if this is a good way to do this or recommend something to beginners. The program launches the PID file when it starts. If he finds one, he throws an exception. Handling this exception verifies that another instance is still running, which may cause a different type of exception. And there is one to handle problems with the IO file. The trick is that X::MyProgram::FoundSemaphore may resume if another program is not running (but left its PID file behind).
class X::MyProgram::FoundSemaphore is Exception { has $.filename; has $.this-pid = $*PID; has $.that-pid = $!filename.lines(1); method gist { "Found an existing semaphore file (pid {.that-pid})" } } class X::MyProgram::StillRunning is Exception { has $.that-pid; has $.os-error; method gist { "This program is already running (pid {self.that-pid})" } } class X::MyProgram::IO::OpenFile is Exception { has $.filename; method gist { "This program is already running (pid {self.that-pid})" } } sub create-semaphore { state $filename = "$*PROGRAM.pid"; END { unlink $filename } die X::MyProgram::FoundSemaphore.new( :filename($filename) ) if $filename.IO.e; my $fh = try open $filename, :w;
exception perl6 resume
brian d foy
source share