关于在DESTROY方法中执行命令的问题

#!/usr/bin/perl package test; sub new() { my $ref = shift; my $class = ref($ref) || $ref; my $self = {}; bless($self, $class); return $self; } sub DESTROY() { my $self = shift; print "this is destroy/n"; my $context = $self->fn(); print $context; } sub fn() { my $self = shift; chdir "/home/fesu"; my $cs = `cat 1.pl`; } sub main() { my $self = shift; print "this is main/nexit 1/n"; exit 1; } package main; my $mytest = new test(); $mytest->main();  

 

这段代码中, 如果屏蔽DESTROY函数中的my $context = $self->fn();     最后exit status就为1, 否则就为0。
     
chinaunix上有位 socyn 提到:

在 perlobj 中找到这么一段,应该就是原因吧

Since DESTROY methods can be called at unpredictable times, it is important that you localise any global variables that the method may update. In particular, localise $@ if you use eval {} and localise $? if you use system or backticks.

 

我也在model_perl上也看到了一句类似的话,说DESTROY方法可能重置$@.

可以通过

local $@; eval {...}; if (my $exception = $@) {...}

立刻复制$@可以预留它的内容。

 

 

要想保证退出状态,我们也可以定义一个$self->{error},在init方法(该方法在new中调用,$self->init();)中初始化它为0。然后在exit 1;之前将它赋值为1,最后在DESTROY方法的最后,返回给$? = $self->{error};

 

 

 

你可能感兴趣的:(exception,perl,System,Class,methods,variables)