Require in Perl, what should I pay attention?

Hi ! Everyone there ! How are you ?
I am a Perl script programmer for business Server at /cgi-bin/. Recently I moved my Server from Perl 5.8.8. (Fedora Code 7) to Perl 5.32.1 (Rocky9.1).

After the movement a pair of Perl scripts (script_main.pl and script_sub.pl), in which I integrated the minor script_sub.pl into the main part (script_main.pl) by using "require".

In the main script_main.pl, I just inserted the following line,

require 'script_sub.pl';

By this way, two scripts worked as if one. While Perl version was 5.8.8. (Redhat Fedora Core 7) this way worked.

However, it appeared that when in Perl 5.32.1 (Rocky9.1) it encountered an error. I studied a little bit of Perl 5.32.1. There are "require", "use", and "import" as possible vocabulary. In my case, it is just joining two pieces of Perl code by the way of "require". What do you suggest in order to chop down into two pieces from a long full Perl script code CGI, and then to join them together ?

Thanks for your help !

5 Comments

Loading external code using "require" is a very dated method - it was introduced in Perl 4. These days, the best approach is to turn "script_sub.pl" into a proper Perl module and load it with "use".

But that's probably a longer job than you have time for right now. So can we fix your immediate problem?

You don't tell us what error you're seeing so this is all guesswork, but I think we can make some educated guesses.

I'm guessing you'll get a generic 500 error in the browser, but if you look in the web server's error log, you'll see a message about not being able to find the code you're trying to load.

If that's the case, then the problem is probably because Perl 5.26 removed the current directory from the list of places where Perl looks for code to load with "use" or "require". This was done for security reasons and was a very good idea.

So the quick fix here is to add back the current directory to the library search path. The easiest way to do this is to add:

use lib '.';

near the top of your code (before the "require" statement).

As I said, this change was made for security reasons - so undoing it is going to introduce a (small) security risk into your application. The best fix is to catch yourself up on the last 25 years of Perl programming best practices.

Dave has spoken. I heartily endorse modernisation.

What does perlcritic say?

I'd probably split the long Perl script into smaller parts and include them together as modules as I find it easier to manage and maintain.

Leave a comment

About Kido Mitsuru

user-pic To ask questions to this blog community