开发者

Does anybody know of a Perl validator

开发者 https://www.devze.com 2023-02-08 23:31 出处:网络
I am very new to Perl and have just started working with it having learnt JavaScript initially. I\'m wondering if there is maybe a validator to check the Perl code (Like Firebug for JS). If anyone开发

I am very new to Perl and have just started working with it having learnt JavaScript initially. I'm wondering if there is maybe a validator to check the Perl code (Like Firebug for JS). If anyone开发者_StackOverflow knows of a reliable one It'd be greatly appreciated. I have Googled it many times with very little success and as we all know with learning a new language mistakes ensure. Thanks in advance!.


What about Perl itself?

perl -c your_program.pl

If you add the following at the beginning of your programs you will avoid many headaches.

use strict;
use warnings;
use diagnostics;

Finally, there's Perl::Critic.


here is an online perl validator

For just checking if the syntax is clean, you could try perl -c filename. For cleaning up the code format-wise, you might want to look at PerlTidy. . You might not need a vaidator. you can do somethings urself.

REDIRECTING ERROR MESSAGES

By default, error messages are sent to STDERR. Most HTTPD servers direct STDERR to the server's error log. Some applications may wish to keep private error logs, distinct from the server's error log, or they may wish to direct error messages to STDOUT so that the browser will receive them.

The carpout() function is provided for this purpose. Since carpout() is not exported by default, you must import it explicitly by saying

 use CGI::Carp qw(carpout);

The carpout() function requires one argument, which should be a reference to an open filehandle for writing errors. It should be called in a BEGIN block at the top of the CGI application so that compiler errors will be caught. Example:

 BEGIN {
    use CGI::Carp qw(carpout);
    open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or
    die("Unable to open mycgi-log: $!\n");
    carpout(LOG);
    }

carpout() does not handle file locking on the log for you at this point. Also, note that carpout() does not work with in-memory file handles, although a patch would be welcome to address that.

The real STDERR is not closed -- it is moved to CGI::Carp::SAVEERR. Some servers, when dealing with CGI scripts, close their connection to the browser when the script closes STDOUT and STDERR. CGI::Carp::SAVEERR is there to prevent this from happening prematurely.

You can pass filehandles to carpout() in a variety of ways. The "correct" way could be topass a reference to a filehandle GLOB:

carpout(*LOG);

the following syntaxes are accepted as well:

carpout(LOG);
carpout(main::LOG);
carpout(main'LOG);
carpout(\LOG);
carpout(\'main::LOG');
... and so on

FileHandle and other objects work as well.

MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW

If you want to send fatal (die, confess) errors to the browser, ask to import the special "fatalsToBrowser" subroutine:

 use CGI::Carp qw(fatalsToBrowser);
    die "Bad error here";

Fatal errors will now be echoed to the browser as well as to the log. CGI::Carp arranges to send a minimal HTTP header to the browser so that even errors that occur in the early compile phase will be seen. Nonfatal errors will still be directed to the log file only (unless redirected with carpout).

*Note that fatalsToBrowser does not work with mod_perl version 2.0 and higher.*

Changing the default message

By default, the software error message is followed by a note to contact the Webmaster by e-mail with the time and date of the error. If this message is not to your liking, you can change it using the set_message() routine. This is not imported by default; you should import it on the use() line:

use CGI::Carp qw(fatalsToBrowser set_message);
set_message("It's not a bug, it's a feature!");

You may also pass in a code reference in order to create a custom error message. At run time, your code will be called with the text of the error message that caused the script to die. Example:

use CGI::Carp qw(fatalsToBrowser set_message);
BEGIN {
sub handle_errors {
my $msg = shift;
print "<h1>Oh gosh</h1>";
print "<p>Got an error: $msg</p>";
}
set_message(\&handle_errors);
}


Because perl is a dynamic language, there are many things it is not possible to validate; for instance, you can call a sub that doesn't exist at compile time, because it could be created during run time. There's no way for a static validator to know if the call is correct or not.

If all you want to know is whether the code compiles, use the perl interpreter itself.

If you want to check the code for adherence to given coding standards, use Perl::Critic.


There are many ways to tackle this problem. You can read http://perldoc.perl.org/perldebug.html to learn how to use the debugger, which in some ways is similar to Firebug. But there are lots of other ways to automatically avoid problems.

  1. Many classes of common errors are caught automatically if you start your code with use strict; use warnings;

  2. http://perltidy.sourceforge.net/ reformats your code and will make many common mistakes easy to catch.

  3. The CPAN module Perl::Critic does automated code analysis to enforce particular style decisions. A consistent style makes errors easier to track down.
  4. Use unit tests. The core module Test::More makes it easy to get started with that.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号