Perl异常自动处理模块-autodie
2014-09-25 14:45:44 阿炯

autodie - Replace functions with ones that succeed or die with lexical scope


autodie模块默认会对一系列Perl内置的函数(用于处理文件、文件句柄、进程间通信和套接字等)自动进行监控异常,也可以自行指定需要监控autodie操作符。从Perl 5.10开始,为人称道的autodie编译指令已经成为标准库的一部分,来处理特定逻辑块中的常见异常,常与Try::Tiny配合使用。

#自动调用die函数:
use autodie;
open my $fh,'<',$filename; # 仍旧会在错误发生时调用die函数

#自行指定施行autodie监管的操作符:
use autodie qw(open system :socket);

#autodie和Try::Tiny协作使用:
use 5.010;
use autodie;
use Try::Tiny;
try {
 open my $fh,'>',$filename; # 发生错误时自动调用die函数
}
catch {
 when('open'){say 'Got an open error'} # 错误的默认变量$_
};

编译指令是靠判别具体操作的类型来工作的。如果Perl内置函数调用了操作系统接口的话,那么中途出现的错误并不是编程人员所能控制的,所以一旦发现系统调用出错,autodie便会自动帮你调用die来料理后事。

use feature qw(switch say);
use Data::Dumper;

eval {
 use autodie;
 open(my $fh, '<', $some_file);
 my @records = <$fh>;
 # Do things with @records...
 close($fh);
};
say Dumper($@);

given ($@) {
 when (undef)   { say "No error";                    }
 when ('open')  { say "Error from open";           }
 when (':io')   { say "Non-open, IO error.";         }
 when (':all')  { say "All other autodie errors."    }
 default        { say "Not an autodie error at all." }
}

The autodie pragma provides a convenient way to replace functions that normally return false on failure with equivalents that throw an exception on failure.

autodie编译指示提供了一种方便的方法来取代函数(通常返回false或失败),即对抛出一个异常的处理。

The autodie pragma has lexical scope, meaning that functions and subroutines altered with autodie will only change their behaviour until the end of the enclosing block, file, or eval.

autodie指令有其作用域,这意味着函数和子程序只会受autodie改变他们的行为,直到封闭块、文件或者eval块结束。

Exceptions produced by the autodie pragma are members of the autodie::exception class. The preferred way to work with these exceptions under Perl 5.10.

使用autodie编译指示产生的异常会存入autodie::exception类中,在在Perl 5.10中处理这些异常是首选方法。

Autodie uses a simple set of categories to group together similar built-ins. Requesting a category type (starting with a colon) will enable autodie for all built-ins beneath that category. For example, requesting :file will enable autodie for close, fcntl, fileno, open and sysopen.

Autodie使用一组简单的相似类别(内建函数)组。一个类别(冒号打头)将使autodie所有内置模板下面的那一类若干函数。例如,':file'将使autodie使用close,fcntl,fileno,open,sysopen这些内置函数。

分类大致如下,其子分类下请参考下文的链接。
:all
 :default
  :io
   :dbm
   :file
   :filesys
   :ipc
     :msg
         :semaphore
     :shm
     :socket
   :threads
  :system

A plain use autodie implies use autodie qw(:default) . Note that system and exec are not enabled by default. system requires the optional IPC::System::Simple module to be installed, and enabling system or exec will invalidate their exotic forms.

直接使用该指令模块即相当于(qw(:default)),默认中没有'system'与'exec'这两个函数。system需要有IPC::System::Simple模块被安装,使'system'与'exec'处于它们奇异的形式。

'use autodie qw(:all)'将会它所有内置函数的错误处理模式。

关于对'system/exec'调用时需要注意的问题

The system built-in is considered to have failed in the following circumstances:

内置函数system在下列情形之一会认为是失败的:
The command does not start.
The command is killed by a signal.
The command returns a non-zero exit value (but see below).

这里使用IPC::System::Simple模块中的system指令来代替内置(CORE)的system。

autodie uses the IPC::System::Simple module to change system.

If you really need to use the exotic form, you can call CORE::system or CORE::exec instead, or use no autodie qw(system exec) before calling the exotic form.

注意:内置的system与IPC中的调用方式不大一样,具体要参考手册实现。

最新版本:


项目主页:http://perldoc.perl.org/autodie.html