Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am the programmer for a decent size company, we have over 50k people that use our system regularly. Because I do not want to shut the website off regularly to make changes, I made a 'upgrade' system that will use the 'mv' command to move the file and over-write the current file. If there are errors it ends up crashing the system so it is not as efficient as I would like it to be. My question is there a way that when I use my upgrade script which sees the files that are there available for upgrade, that can require those in a way that will not error out if there are errors, but will simply report any errors the file contains?

For example it detects the presence of /home/path/to/upgrade/files/my_code.conf
which could contain just perl code in this format:
#File Name: my_code.conf ## Subroutines in this file: ### sub1 ### sub2 ### sub3 sub sub1 { # Some code return 1; } sub sub2 { # Some code return 1; } sub sub3 { # Some code return 1; } 1;
That would be the file. So how can Perl just run a way to call a 'require' on that file to pull it in, but only for checking for errors and if it contains any just give me the results instead of actually doing a require and then getting a software error?

Sorry if that is hard to understand I can clarify it if you need me to.

Thanks,
Rich
  • Comment on question about checking a script for errors from within a perl program
  • Download Code

Replies are listed 'Best First'.
Re: question about checking a script for errors from within a perl program
by drench (Beadle) on Mar 21, 2009 at 07:18 UTC
    It sounds like you want to enclose your 'require' in an eval block to trap exceptions.
    eval { require '/home/path/to/upgrade/files/my_code.conf'; }; if ($@) { warn "The require of 'my_code.conf' failed. Here's why: $@\n"; }
    "perldoc -f eval" for more information.
Re: question about checking a script for errors from within a perl program
by Your Mother (Archbishop) on Mar 21, 2009 at 07:37 UTC

    If I'm following correctly, all you need to do is wrap the require call in an eval. If it is successful, the code loaded and can be used. If it's not, I don't think any of the code can have loaded (a more perlgutsy monk will correct me if that's wrong) and you'll get the error in $@. This snippet might help you get started:

    perl -le 'eval { require(shift) }; print $@ ? "ERROR: $@" : "Loaded fi +ne!"' /home/path/to/upgrade/files/my_code.conf ERROR: Can't locate /home/path/to/upgrade/files/my_code.conf in @INC ( +@INC contains: /usr/local/lib/perl5/5.10.0/darwin-2level /usr/local/l +ib/perl5/5.10.0 /usr/local/lib/perl5/site_perl/5.10.0/darwin-2level / +usr/local/lib/perl5/site_perl/5.10.0 /usr/local/lib/perl5/site_perl/5 +.8.2 /usr/local/lib/perl5/site_perl .) at -e line 1. perl -lwe 'eval { require(shift) }; print $@ ? "ERROR: $@" : "Loaded f +ine!"' /usr/local/lib/perl5/5.10.0/CGI.pm Loaded fine!

    (update: didn't see drench's reply yet when I started mine.)