I am calling many Perl scripts in my Bash script (sometimes from csh also).
我在我的Bash脚本中调用了许多Perl脚本(有时也来自csh)。
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.
在Bash脚本的开头,我想进行一个测试,检查所有Perl脚本是否没有任何编译错误。
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.
实现此目的的一种方法是实际从Bash脚本调用Perl脚本并在管道日志文件中grep“编译错误”,但由于在代码中的不同点调用不同的Perl脚本,这会变得混乱,所以我想要在Bash脚本的最开始执行此操作。
Is there a way to check if the Perl script has no compilation error?
有没有办法检查Perl脚本是否没有编译错误?
4 个解决方案
#1
60
Beware!!
谨防!!
Using the below command to check compilation errors in your Perl program can be dangerous.
使用以下命令检查Perl程序中的编译错误可能很危险。
$ perl -c yourperlprogram
Randal has written a very nice article on this topic which you should check out
兰德尔写了一篇关于这个主题的非常好的文章,你应该看一下
- Sanity-checking your Perl code (Linux Magazine Column 91, Mar 2007)
- 完善性检查Perl代码(Linux Magazine专栏91,2007年3月)
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本身,传递仅编译开关:
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.
对于此操作,perl编译程序,但在执行阶段之前停止。这意味着程序文本的每个部分都被转换为代表工作程序的内部数据结构,但我们实际上并没有执行任何代码。如果有任何语法错误,我们会收到通知,编译将中止。
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块(包括它们的分层表达,use指令),在理论上安全的“语法检查”期间可能已经执行了一些Perl代码。例如,如果您的代码包含:
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:
然后你会看到那条消息,即使在perl -c期间!对于那些认为“仅编译”意味着“不执行代码”的人来说,这有点令人惊讶。考虑包含以下内容的代码:
BEGIN { system "rm", "-rf", "/" }
and you'll see the problem with that argument. Oops.
你会看到这个论点的问题。哎呀。
#2
4
Apart from perl -c program.pl
, it's also better to find warnings using the command:
除了perl -c program.pl之外,使用该命令查找警告也更好:
perl -w program.pl
For details see: http://www.perl.com/pub/2004/08/09/commandline.html
有关详细信息,请参阅:http://www.perl.com/pub/2004/08/09/commandline.html
#3
0
I use the following part of a bash func for larger perl projects :
我使用bash函数的以下部分来处理更大的perl项目:
# foreach perl app in the src/perl dir
while read -r dir ; do
echo -e "\n"
echo "start compiling $dir ..." ;
cd $product_instance_dir/src/perl/$dir ;
# run the autoloader utility
find . -name '*.pm' -exec perl -MAutoSplit -e 'autosplit($ARGV[0], $ARGV[1], 0, 1, 1)' {} \;
# foreach perl file check the syntax by setting the correct INC dirs
while read -r file ; do
perl -MCarp::Always -I `pwd` -I `pwd`/lib -wc "$file"
# run the perltidy inline
# perltidy -b "$file"
# sleep 3
ret=$? ;
test $ret -ne 0 && break 2 ;
done < <(find "." -type f \( -name "*.pl" -or -name "*.pm" \))
test $ret -ne 0 && break ;
echo "stop compiling $dir ..." ;
echo -e "\n\n"
cd $product_instance_dir ;
done < <(ls -1 "src/perl")
#4
-1
When you need to check errors/warnings before running but your file depends on mutliple other files you can add option -I:
如果您需要在运行之前检查错误/警告,但您的文件依赖于mutliple其他文件,您可以添加选项-I:
perl -I /path/to/dependency/lib -c /path/to/file/to/check
Edit: from man perlrun
编辑:来自man perlrun
Directories specified by -I are prepended to the search path for modules (@INC).
由-I指定的目录前置于模块的搜索路径(@INC)。
#1
60
Beware!!
谨防!!
Using the below command to check compilation errors in your Perl program can be dangerous.
使用以下命令检查Perl程序中的编译错误可能很危险。
$ perl -c yourperlprogram
Randal has written a very nice article on this topic which you should check out
兰德尔写了一篇关于这个主题的非常好的文章,你应该看一下
- Sanity-checking your Perl code (Linux Magazine Column 91, Mar 2007)
- 完善性检查Perl代码(Linux Magazine专栏91,2007年3月)
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本身,传递仅编译开关:
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.
对于此操作,perl编译程序,但在执行阶段之前停止。这意味着程序文本的每个部分都被转换为代表工作程序的内部数据结构,但我们实际上并没有执行任何代码。如果有任何语法错误,我们会收到通知,编译将中止。
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块(包括它们的分层表达,use指令),在理论上安全的“语法检查”期间可能已经执行了一些Perl代码。例如,如果您的代码包含:
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:
然后你会看到那条消息,即使在perl -c期间!对于那些认为“仅编译”意味着“不执行代码”的人来说,这有点令人惊讶。考虑包含以下内容的代码:
BEGIN { system "rm", "-rf", "/" }
and you'll see the problem with that argument. Oops.
你会看到这个论点的问题。哎呀。
#2
4
Apart from perl -c program.pl
, it's also better to find warnings using the command:
除了perl -c program.pl之外,使用该命令查找警告也更好:
perl -w program.pl
For details see: http://www.perl.com/pub/2004/08/09/commandline.html
有关详细信息,请参阅:http://www.perl.com/pub/2004/08/09/commandline.html
#3
0
I use the following part of a bash func for larger perl projects :
我使用bash函数的以下部分来处理更大的perl项目:
# foreach perl app in the src/perl dir
while read -r dir ; do
echo -e "\n"
echo "start compiling $dir ..." ;
cd $product_instance_dir/src/perl/$dir ;
# run the autoloader utility
find . -name '*.pm' -exec perl -MAutoSplit -e 'autosplit($ARGV[0], $ARGV[1], 0, 1, 1)' {} \;
# foreach perl file check the syntax by setting the correct INC dirs
while read -r file ; do
perl -MCarp::Always -I `pwd` -I `pwd`/lib -wc "$file"
# run the perltidy inline
# perltidy -b "$file"
# sleep 3
ret=$? ;
test $ret -ne 0 && break 2 ;
done < <(find "." -type f \( -name "*.pl" -or -name "*.pm" \))
test $ret -ne 0 && break ;
echo "stop compiling $dir ..." ;
echo -e "\n\n"
cd $product_instance_dir ;
done < <(ls -1 "src/perl")
#4
-1
When you need to check errors/warnings before running but your file depends on mutliple other files you can add option -I:
如果您需要在运行之前检查错误/警告,但您的文件依赖于mutliple其他文件,您可以添加选项-I:
perl -I /path/to/dependency/lib -c /path/to/file/to/check
Edit: from man perlrun
编辑:来自man perlrun
Directories specified by -I are prepended to the search path for modules (@INC).
由-I指定的目录前置于模块的搜索路径(@INC)。