Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Perl script doesn't have any compilation errors?

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?

like image 661
user13107 Avatar asked Oct 16 '12 05:10

user13107


People also ask

Where are Perl errors logged?

Perl scripts that cause a “500 Internal Server Error” or “403 Forbidden” error message are logged in the web server's error log. You can use cPanel to view the error log for your web site.

Can Perl scripts be compiled?

Since version 5.005, Perl has shipped with a module capable of inspecting the optimized parse tree ( B ), and this has been used to write many useful utilities, including a module that lets you turn your Perl into C source code that can be compiled into an native executable.

How do I test a Perl script in Windows?

Just open a command prompt (in Windows, just type cmd in the run dialog and press Enter. If you're on a Mac or on Linux, open a terminal window). and press Enter. If Perl is installed, you receive a message indicating its version.


1 Answers

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

  • Sanity-checking your Perl code (Linux Magazine Column 91, Mar 2007)

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.

like image 82
Chankey Pathak Avatar answered Sep 22 '22 02:09

Chankey Pathak