如何检查Perl脚本是否没有任何编译错误? [英] How to check if a Perl script doesn't have any compilation errors?

查看:108
本文介绍了如何检查Perl脚本是否没有任何编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Bash脚本中调用许多Perl脚本(有时来自csh)。



在Bash脚本的开头,我想要一个测试,如果所有的Perl脚本没有任何编译错误。



这样做的一种方法是实际上从Bash脚本调用Perl脚本和 grep 在管道日志文件中的编译错误,但是这变得杂乱,因为不同的Perl脚本在代码中的不同点被调用,所以我想在Bash开始的时候这样做脚本。



有没有办法检查Perl脚本是否没有编译错误?

解决方案

请小心!



使用以下命令检查Perl程序中的编译错误可能是危险的。

  $ perl -c yourperlprogram 



Randal写了一篇很好的文章,你应该查看





从他的文章引用:


也许我们可以告诉的最简单的事情是是有效的。为此,
我们调用perl本身,传递只编译开关:

  perl -c我们的程序

对于此操作,perl编译程序
,但在执行阶段之前停止。这意味着程序文本的每个
部分被转换为表示工作程序的内部数据
结构,但是我们实际上没有执行任何代码
。如果有任何语法错误,我们会被通知,
编译中止。



实际上,这是一个谎言。由于BEGIN块(包括
他们的分层的表兄弟,使用指令),一些Perl代码可能有
在这个理论上安全的语法检查执行。对于
示例,如果您的代码包含:

  BEGIN {warnHello,world!\\\
}

那么你将看到该消息,
甚至在perl -c!这对于
认为只编译意味着不执行代码的人来说有点令人惊讶。考虑包含以下内容的
代码:

  BEGIN {systemrm,-rf,/ 

,你会看到
参数的问题。糟糕。



I am calling many Perl scripts in my Bash script (sometimes from csh also).

At the start of the Bash script I want to put a test which checks if all the Perl scripts are devoid of any compilation errors.

One way of doing this would be to actually call the Perl script from the Bash script and grep for "compilation error" in the piped log file, but this becomes messy as different Perl scripts are called at different points in the code, so I want to do this at the very start of the Bash script.

Is there a way to check if the Perl script has no compilation error?

解决方案

Beware!!

Using the below command to check compilation errors in your Perl program can be dangerous.

$ perl -c yourperlprogram

Randal has written a very nice article on this topic which you should check out

Quoting from his article:

Probably the simplest thing we can tell is "is it valid?". For this, we invoke perl itself, passing the compile-only switch:

perl -c ourprogram

For this operation, perl compiles the program, but stops just short of the execution phase. This means that every part of the program text is translated into the internal data structure that represents the working program, but we haven't actually executed any code. If there are any syntax errors, we're informed, and the compilation aborts.

Actually, that's a bit of a lie. Thanks to BEGIN blocks (including their layered-on cousin, the use directive), some Perl code may have been executed during this theoretically safe "syntax check". For example, if your code contains:

BEGIN { warn "Hello, world!\n" } 

then you will see that message, even during perl -c! This is somewhat surprising to people who consider "compile only" to mean "executes no code". Consider the code that contains:

BEGIN { system "rm", "-rf", "/" } 

and you'll see the problem with that argument. Oops.

这篇关于如何检查Perl脚本是否没有任何编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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