bash 如何在 Perl 5.8.8 中使用“说”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8260972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:06:17  来源:igfitidea点击:

How can I use 'say' with Perl 5.8.8?

perlfilebashsearch

提问by G. Kashtanov

I'm assigned to a project (PHP/MySQL) that needs some review and possibly fixes. As a part of it it is planned to check correctness of all variables that we get via GET and POST. We have a bunch of php and tpl files that have several thouthands of lines of code all together, so it would be painful to search for all $_GET[...] and $_POST[...] references manually. So I tried doing this:

我被分配到一个需要一些和可能修复的项目(PHP/MySQL)。作为其中的一部分,计划检查我们通过 GET 和 POST 获得的所有变量的正确性。我们有一堆 php 和 tpl 文件,它们总共有数千行代码,因此手动搜索所有 $_GET[...] 和 $_POST[...] 引用会很痛苦。所以我尝试这样做:

find . -name "*.php" -or -name "*.tpl" |
xargs perl -ne 'use feature say; say  while m/($_(GET|POST)\[[\s]*[\S]+[\s]*\])/g;' |
sort -u

Basically it creates a list of all $_GET[...] and $_POST[...] references in the project, and then deletes the repeated values from it. But it didn't work because I have perl 5.8.8 on my development machine, which does not support 5.10+ feature 'say', and our system administrator said that upgrade is undesired. I'm not sure why, but he's the boss.

基本上它会创建项目中所有 $_GET[...] 和 $_POST[...] 引用的列表,然后从中删除重复的值。但它不起作用,因为我的开发机器上有 perl 5.8.8,它不支持 5.10+ 功能“say”,我们的系统管理员说升级是不受欢迎的。我不知道为什么,但他是老板。

So, is there a way to replace 'say' with some other code, or maybe even replace Perl with another tool?

那么,有没有办法用其他代码替换“say”,或者甚至用其他工具替换 Perl?

回答by Zaid

Don't forget that it's very easy to emulate say:

不要忘记它很容易模仿say

sub say { print @_, "\n" }

Just add it to the beginning of the Perl code and use as you normally would.

只需将它添加到 Perl 代码的开头并像往常一样使用即可。

回答by JRFerguson

Perl 5.10 added 'say' which is a substitute for 'print' that automatically adds a newline to the output. Hence you can write"

Perl 5.10 添加了'say',它替代了'print',它会自动向输出添加换行符。因此你可以写“

say "hello";

...instead of having to write:

...而不必写:

print "hello\n";

Simply drop the 'use feature say;' and replace 'say $1' with:

只需删除“使用功能说;” 并将“说 $1”替换为:

print "\n";

回答by TLP

The -loption without octal adds a newline to the end of print statements. From perlrun:

-l不带八进制的选项在打印语句的末尾添加一个换行符。从perlrun

enables automatic line-ending processing. It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted, sets $\ to the current value of $/.

启用自动行结束处理。它有两种不同的效果。首先,当与 -n 或 -p 一起使用时,它会自动截取 $/(输入记录分隔符)。其次,它将 $\(输出记录分隔符)分配为 octnum 的值,以便任何打印语句都将重新添加该分隔符。如果省略 octnum,则将 $\ 设置为 $/ 的当前值

So..

所以..

perl -lne ... print  ...

Will replace say.

将取代say.

回答by Tanktalus

But it didn't work because I have perl 5.8.8 on my development machine, which does not support 5.10+ feature 'say',

但它不起作用,因为我的开发机器上有 perl 5.8.8,它不支持 5.10+ 功能“说”,

Perl 5.8.8 was released on Jan 31, 2006. Since then, we've seen 5.10, 5.10.1, 5.12, 5.12.1, 5.12.2, 5.12.3, 5.12.4, 5.14, 5.14.1, 5.14.2, ... and I might be out of date. Three major revisions (5.10, 5.12, 5.14) with fixes each, since 5.8.8 came out. Deprecation cycles have occurred, and you've not had a chance to experience them to help clean up your code.

Perl 5.8.8 于 2006 年 1 月 31 日发布。从那时起,我们看到了 5.10、5.10.1、5.12、5.12.1、5.12.2、5.12.3、5.12.4、5.14、5.14.1、5.14.1、5.12.1 .2, ... 我可能已经过时了。自 5.8.8 发布以来,三个主要修订版(5.10、5.12、5.14)均进行了修复。弃用周期已经发生,您还没有机会体验它们来帮助清理您的代码。

The simple solution is to upgrade.

简单的解决方案是升级。

and our system administrator said that upgrade is undesired. I'm not sure why, but

我们的系统管理员说升级是不受欢迎的。我不确定为什么,但是

And, so? It's not like he's the boss of ...

所以?他又不是……的老板。

he's the boss.

他是老板。

Oh.

哦。

Well, I'm not one that is well known for listening to bosses when they're being silly or plain incorrect. Then again, I never advocate doing what I do :-)

嗯,我不是一个以倾听老板愚蠢或完全不正确而闻名的人。再说一次,我从不提倡做我所做的:-)

If your boss is concerned with upgrading the system perl, he's right. That's undesirable. (I suggest getting a newer distro, but if you're on AIX, you're stuck as newer AIX's still ship that old level of perl.) But there's no reason why you have to upgrade the system perl just to upgrade perl.

如果你的老板关心升级系统 perl,他是对的。这是不可取的。(我建议获得更新的发行版,但是如果您使用的是 AIX,您会被卡住,因为新的 AIX 仍然提供旧级别的 perl。)但是没有理由仅仅为了升级 perl 而必须升级系统 perl。

You can have more than one perl installed. As long as you don't touch /usr/bin/perl, and you shouldn't as that should be the system perl at all times, you should be fine. I have installed perls 5.8.8, 5.10.1, 5.12.0, 5.12.1, 5.12.2, 5.14.0, 5.14.1, and 5.14.2 all in ~/perl/$version, created symlinks in ~/bin for each one (e.g., perl5.8.8 is a symlink to the 5.8.8 perl binary), and now I can use developmentperls for their new features while still being able to test on older perls when necessary. And, because I'm installing to my own home directory, I don't need sysadmin access. If you're not on Linux, you willneed a compiler. (Even on Linux you may need some development packages installed, e.g., on SLES we often need to install -devel RPMs to get extra modules to compile.) But this is likely a lower bar to meet for your sysadmin than overwriting the system perl (which he's right to warn against).

您可以安装多个 perl。只要你不接触/usr/bin/perl,你不应该,因为它应该一直是系统perl,你应该没问题。我已经在 ~/perl/$version 中安装了 perls 5.8.8、5.10.1、5.12.0、5.12.1、5.12.2、5.14.0、5.14.1 和 5.14.2,在 ~/ 中创建了符号链接bin(例如,perl5.8.8 是 5.8.8 perl 二进制文件的符号链接),现在我可以将开发perls 用于他们的新功能,同时在必要时仍然能够在旧 perls 上进行测试。而且,因为我要安装到我自己的主目录,所以我不需要 sysadmin 访问权限。如果你不在 Linux 上,你需要编译器。(即使在 Linux 上,您可能需要安装一些开发包,例如,在 SLES 上,我们经常需要安装 -devel RPM 来获得额外的模块来编译。)但这对您的系统管理员来说可能比覆盖系统 perl 更低(他警告他是对的)。