perl编程 2009-08-20 19:16:42 阅读262 评论0 字号:大中小 订阅
近日分析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一样的。这个模块能产生更多的错误信息。
总结以上是内存溢出为你收集整理的perl Carp模块使用举例(转)全部内容,希望文章能够帮你解决perl Carp模块使用举例(转)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)