登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

有事儿您说话!

爱吃,爱旅游,爱什么,好的事儿我都爱

 
 
 

日志

 
 
 
 

perl Carp模块使用举例  

2009-08-20 19:16:42|  分类: perl编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

近日分析extmail的源代码,看到Carp的perl模块,有些搞不懂,perldoc一把,了解了其用法,放在这里备忘!

Carp模块提供了carp(),croak(),confess(),cluck(),shortmess(),longmess()六个函数,这些函数产生的错误信息与warn(),die()类似。不同之处在于,后者标识的是出现错误的行号,前者产生调用错误的子程序命令行位置。

# script_1 source code: warn and die function

#!/usr/bin/perl
package MyPackage ;
sub my_fun {
        print "This is warn and die function output message!\n" ;
        warn("warn") ;
        die("die") ;
}

程序输出:

This is warn and die function output message!
warn at carp.pl line 8.
die at carp.pl line 9.

script 2:source code --carp croak

#!/usr/bin/perl
package MyPackage ;
use Carp ;
sub my_carp{
        print "This is Carp Module function carp croak and confess output message!\n" ;
        carp("carp") ;
        croak("croak") ;
        confess("confess") ;
}
package main ;
 MyPackage::my_carp() ;

程序输出:

[root@mail bash]# perl carp.pl
This is Carp Module function carp croak and confess output message!
carp at carp.pl line 20
croak at carp.pl line 20

script 3 source code -- confess

#!/usr/bin/perl
use Carp ;
 
sub one {
        two() ;
}
sub two {
        three() ;
}
sub three {
        confess("confess") ;
}
one() ;

程序输出:

[root@mail bash]# perl carp_confess.pl
confess at carp_confess.pl line 12
        main::three() called at carp_confess.pl line 9
        main::two() called at carp_confess.pl line 6
        main::one() called at carp_confess.pl line 14

script 4 source code :-- cluck shortmess and longmess

use Carp qw(cluck) ;
cluck "This is how we got here!" ;
print STDOUT Carp::shortmess("This will have caller's details added") ;
print STDOUT Carp::longmess("This will have stack backtrace added") ;

 

程序输出:

This is how we got here! at new_carp.pl line 3
This will have caller's details added at new_carp.pl line 5
This will have stack backtrace added at new_carp.pl line 6

 

大概就这么大了。就跟使用warn和die一样的。这个模块能产生更多的错误信息。

 

  评论这张
 
阅读(3689)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018