3

I have to optimize an intranet written in Perl (about 3000 files). The first thing I want to do is enable warnings "-w" or "use warnings;" so I can get rid of all those errors, then try to implement "use strict;".

Is there a way of telling Perl to use warnings all the time (like the settings in php.ini for PHP), without the need to modify each script to add "-w" to it's first line?

I even thought to make an alias for /usr/bin/perl, or move it to another name and make a simple script instead of it just to add the -w flag (like a proxy).

How would you debug it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Radu Maris
  • 5,648
  • 4
  • 39
  • 54
  • Hard to accept an answer where all 3 helped ... @tchrist and @Michael Carman give me an answer to what I was looking for in the first place, but @Jonathan Leffler gave a good advice "don't do that" (and gave a good explanation for my second question: how would you debug it ?) ..., I hope nobody will get upset if I will accept @Jonathan Leffler answer. Thank you all for your help. – Radu Maris Jan 19 '11 at 09:42

3 Answers3

12

Well…

You could set the PERL5OPT envariable to hold -w. See the perlrun manpage for details. I hope you’ll consider tainting, too, like -T or maybe -t, for security tracking.

But I don’t envy you. Retrofitting code developed without the benefit of use warnings and use strict is usually a royal PITA.

I have something of a standard boiler-plate I use to start new Perl programs. But I haven’t given any thought to one for CGI programs, which would likely benefit from some tweaks against that boiler-plate.

Community
  • 1
  • 1
tchrist
  • 78,834
  • 30
  • 123
  • 180
  • I now tested the PERL5OPT approach, maibe I didn't understand the documentation, but I run a export command in terminal "export PERL5OPT=w", after that every script I run in that terminal session should benefit of that -w switch (that works), but when I load the scripts from apache, it has no efect, there is a way of doind this for apache, some settings in httpd.conf or anywhere ? – Radu Maris Jan 19 '11 at 12:07
9

Retrofitting warnings and strict is hard. I don't recommend a Big Bang approach, setting warnings (let alone strictures) on everything. You will be inundated with warnings to the point of uselessness.

You start by enabling warnings on the modules used by the scripts (there are some, aren't there?), rather than applying warnings to everything. Get the core clean, then get to work on the periphery, one unit at a time. So, in fact, I'd recommend having a simple (Perl) script that simply finds a line that does not start with a hash and adds use warnings; (and maybe use strict; too, since you're going to be dealing with one script at a time), so you can do the renovations one script at a time.

In other words, you will probably be best off actually editing each file as you're about to renovate it.

I'd only use the blanket option to make a simple assessment of the scope of the problem: is it a complete and utter disaster, or merely a few peccadilloes in a few files. Sadly, if the code was developed without warnings and strict, it is more likely to be 'disaster' than 'minimal'.

You may find that your predecessors were prone to copy and paste and some erroneous idioms crop up repeatedly in copied code. Write a Perl script that fixes each one. I have a bunch of fix* scripts in my personal bin directory that deal with various changes - either fixing issues created by recalcitrant (or, more usually, simply long departed) colleagues or to accommodate my own changing standards.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I allready fixed the modules, I have one (cgilib.pl) that every script uses, and another 8 that are used by diferent scripts. Now I have to dive in the pool of scripts to fix them to. I did another thing, I made cgilib.pl log every path of scripts that are accesing it (using $0), so far just 273 script are regularly used, so I will start with them, I have a felling that about 70% of the script are old and not used at all, if they will not be registred in the log, I will copy them to the backup server and delete them from the production server. – Radu Maris Jan 19 '11 at 08:13
  • @radu: That sounds fairly typical - a classic 80:20 rule (though in this case it is nearer 90:10) with 20% of the scripts doing most of the work and the rest being largely forgotten or very seldom used. It is very sensible of you to log which scripts are in use - it tells you where to concentrate your efforts. Even within the 273, there are probably going to be 20-50 really heavily used and the remainder less heavily used. Good luck! – Jonathan Leffler Jan 19 '11 at 08:23
5

You can set warnings and strictures for all Perl scripts by adding -Mwarnings -Mstrict to your PERL5OPT environment variable. See perlrun for details.

Michael Carman
  • 30,628
  • 10
  • 74
  • 122