近日分析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一样的。这个模块能产生更多的错误信息。