Perl 서브 루틴에서 잡힌 오류와 매치 오류를 모두 처리하려면 어떻게해야합니까?
-
22-08-2019 - |
문제
이것은 후속 조치입니다 "수정할 수없는 Perl 라이브러리에서 어떻게 '다이'전화를 걸 수 있습니까?".
라이브러리를 부르는 서열이 있습니다. 이 서브 루틴 안에있는 각 호출을 평가 {}로 소개하는 대신, 나는 단지 죽을 수있게하고, 내 서브 루틴이라고하는 레벨에서 Eval {}를 사용합니다.
my $status=eval{function($param);};
unless($status){print $@; next;}; # print error and go to
# next file if function() fails
그러나 function ()에서 잡을 수있는 오류 조건이 있습니다. 서브 루틴과 호출 루틴의 오류 캐칭을 설계하는 가장 적절한/우아한 방법은 무엇입니까?
해결책
블록 평가는 중첩 될 수 있습니다.
sub function {
eval {
die "error that can be handled\n";
1;
} or do {
#propagate the error if it isn't the one we expect
die $@ unless $@ eq "error that can be handled\n";
#handle the error
};
die "uncaught error";
}
eval { function(); 1 } or do {
warn "caught error $@";
};
다른 팁
나는 당신이 무엇을하고 싶은지 잘 모르겠지만, 당신은 핸들러로 그것을 할 수 있다고 생각합니다.
$SIG{__DIE__} = sub { print $@ } ;
eval{ function($param); 1 } or next;
제휴하지 않습니다 StackOverflow