package 中的函数都要加上命名空间吗?
时间:2011-02-01
来源:互联网
本帖最后由 yakczh 于 2011-02-01 12:52 编辑
我写了一个package 里面的函数用到md5
比如
复制代码
然后在另个perl文件中 require这个包
a.pl
复制代码
提示 Undefined subroutine &Utils::md5_hex called at Utils.pm
我写了一个package 里面的函数用到md5
比如
- use Digest::MD5 qw(md5 md5_hex md5_base64);
- package Utils;
-
- sub aaa{
- my $str=shift;
- return md5_hex($str);
- }
a.pl
- require "Utils.pm";
-
- print Utils::aaa("som");
作者: yakczh 发布时间: 2011-02-01
package里这2句调一下顺序:
复制代码
要理解什么是“包”。
- package Utils;
- use Digest::MD5 qw(md5 md5_hex md5_base64);
作者: 兰花仙子 发布时间: 2011-02-01
本帖最后由 yakczh 于 2011-02-01 12:53 编辑
包应该起命名空间的作用吧
还有自己写包的话 一个包最后都要写上一句
复制代码
不然导入包的文件会提示
Utils.pm did not return a true value at :
这是什么道理
包应该起命名空间的作用吧
还有自己写包的话 一个包最后都要写上一句
- 1;
Utils.pm did not return a true value at :
这是什么道理
作者: yakczh 发布时间: 2011-02-01
本帖最后由 zhlong8 于 2011-02-01 13:54 编辑
perldoc -f require 中有这么一句
其中给的等效代码用的是
复制代码
而 do 返回 undef 表示失败,否则返回最后表达式的值
其中 require 的等效代码只检测了 $@ 编译错误,而没有检查 $! 表示的 IO 错误
require 的错误处理是这样的
复制代码
意味着编译错误即使被捕获也不会重新导入了,因为没有 delete $INC{$filename},而下面的 $result 则表明是 IO 错误,如果被捕获还可以重新导入相应模块。至于 require 为什么不直接检测 $! 我也不明白现在
perldoc -f require 中有这么一句
QUOTE:
The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1; , in case you add more statements.
其中给的等效代码用的是
- $result = do $realfilename;
QUOTE:
If do cannot read the file, it returns undef and sets $! to the error. If do can read the file but cannot compile it, it returns undef and sets an error message in $@ . If the file is successfully compiled, do returns the value of the last expression evaluated.
其中 require 的等效代码只检测了 $@ 编译错误,而没有检查 $! 表示的 IO 错误
require 的错误处理是这样的
- if ($@) {
- $INC{$filename} = undef;
- die $@;
- } elsif (!$result) {
- delete $INC{$filename};
- die "$filename did not return true value";
- } else {
- return $result;
- }
作者: zhlong8 发布时间: 2011-02-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28