什么是有效的Perl模块返回值? [英] What are valid Perl module return values?

查看:95
本文介绍了什么是有效的Perl模块返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl中的常见做法当然是使用1;结束模块,以便随后可以检查对require的调用是否成功.是否有任何原因不能将返回值设为另一个真实值?在我的测试中,它似乎不会引起任何问题,但是我想知道是否有人遇到任何问题(例如某些其他模块或编译指示或任何期望值实际为1的东西,而不仅仅是是).

The common practice in Perl is of course to end modules with 1; so that a call to require can then be checked for success. Is there any reason that the return value couldn't be another true value? In my testing, it does not see to cause any problems, but I'd like to know if anyone has run across any issues (such as some other modules or pragmas or anything that expect the value actually to be 1 and not just true).

流行的观点认为,由于它只能运行一次(提示),因此该代码示例不见了.似乎共识是,它可以安全地返回任何真实值,但永远不要依赖于调用代码中的那个值,因为require在第一次加载后将返回1

By popular opinion, and since it will only work once (good tip), the code example is gone. Seems the consensus is that its safe to return any true value, but never to rely on the that value in calling code since require will return 1 after the first loading

推荐答案

一段时间以来,我一直在把愚蠢的东西放在模块末尾.没有害处,还有一点复活节彩蛋. uny2k 有用地以"Yes, this code is a joke."结尾

I've been putting silly things at the end of my modules for a while. There's no harm and its a little Easter egg. uny2k helpfully ends with "Yes, this code is a joke." Class::Fields is chock full of them.

更进一步,有时在记录了某个函数以返回truefalse的情况下,对于true,我将返回1以外的值.这是为了惩罚写if foo() == 1的人,当他们的意思是if foo()时.大约在我写use constant TRUE => 1==1; use constant FALSE => !TRUE;

Taking it one step further, sometimes when a function is documented to return true and false I will return something other than 1 for true. This is to punish people who write if foo() == 1 when they mean if foo(). This was around the same period I was writing use constant TRUE => 1==1; use constant FALSE => !TRUE;

我已经看到了生产代码中使用的模块的返回值.我不记得是什么原因.开发人员的逻辑遭受了折磨.我相信这是一种不希望只写一行而不是两行的事情.我不记得他为什么不只导出它.

I have seen the return value of a module used in production code. I don't recall exactly why. The developer's logic was... tortured. I believe it was something along the lines of not wanting to have to write just one line instead of two. I don't remember why he didn't just export it.

同一位开发人员使用%_传递参数(*_符号是全局跨包),并在map语句内编写了150条线图语句.

This was the same developer who used %_ to pass arguments around (the *_ symbol is global across packages) and wrote 150 line map statements inside map statements.

除了混淆之外,使用返回值的危险在于它只能工作一次.

The danger of using the return value, aside from obfuscation, is that it only works once.

$ cat Foo.pm
package Foo;

return "Basset hounds got long ears";

$ cat test.plx
#!/usr/bin/perl -w

print require Foo, "\n";
print require Foo, "\n";

$ perl -I. test.plx
Basset hounds got long ears
1

第一次调用require会评估Foo.pm并返回返回值.第二个调用在%INC中已经看到它,并且只返回true.而且,您甚至无法确定自己是第一位需要该代码的人.您可以使用do "Foo.pm"来解决此问题,但是现在每次都在重新加载模块时显示有关重新定义的例程,性能问题以及可能重新初始化全局变量的警告.这不值得.

The first call to require evaluates Foo.pm and returns the return value. The second call sees its already in %INC and just returns true. And you can't even be sure that you're the first thing to require the code. You can get around this with do "Foo.pm" but now you're reloading the module each time with warnings about redefined routines, problems for performance and possibly reinitializing globals. Its not worth it.

这篇关于什么是有效的Perl模块返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆