Cleaning up a perl program (using strict); resources? (was: Solved (very odd!?) was: Losing a file handle in perl)

Alex Beamish talexb-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Wed Dec 15 03:19:17 UTC 2004


On Tue, 14 Dec 2004 20:54:46 -0500, Madison Kelly <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org> wrote:
> Stewart C. Russell wrote:
> > Lennart Sorensen wrote:
> >
> >>
> >> perl doesn't do that as far as I know (unless that is one of the
> >> things -w tells you about).
> >
> >
> > There is no valid excuse for not having:
> >
> > use strict;
> > use warnings;
> >
> > in your Perl programs. Ever.
> >
> >  Stewart
> >
> > (well, perhaps in throwaway one-liners, but I've seen them integrated
> > into production systems ...)
> 
> Hi,
> 
>    Well, I have heard so many people echo the same thing but until now I
> was more concerned with "making it work". Now it works though and I am
> pretty heavily re-writting the code. I decided that since I just
> finished the re-write of the second major section of the program and
> have a few sections (separate files) still to do now would be a good
> time to enable 'use strict;' and 'use warnings;'.

You've been badgered already about using strict .. but if you're
writing a piece of Perl code from scratch, at the very least you want

#!/usr/bin/perl -w

at the top to turn on warnings -- always. That first line has replaced
something I usde to type a lot,

#include <stdio.h>

back when I was a full-time C guy. I don't always use strict, but it
is a good idea -- it forces you to declare your variables, and makes
you be more explicit about what your program does. Remember Murphy's
Law -- if it's gonna go wrong, it's probably gonna go wrong in the
direction you didn't want it to go.

>    Doing this has thrown a LOT of errors and warnings which I now need
> to work through. Can you recommend resources I could read that would
> help me decypher these errors and warnings? For example, I am getting
> stuff like:
> 
> Global symbol "$say_type" requires explicit package name at
> /usr/share/tle-bu/cgi-bin/part-conf.cgi line 1002.

P. 943, the Camel. You've used a symbol without indicating whether
it's a local variable (declared with my), a module variable (declared
with our) or a variabled from another module (declared with our in the
module, and then exported).

> and
> 
> Bareword "true" not allowed while "strict subs" in use at
> /usr/share/tle-bu/cgi-bin/part-conf.cgi line 134.

P. 925, the Camel. It's hard to know exactly what the context is, but
if this is a test for 'truth', then

  if($condition) {
    #  Do something
  } else {
    # or do something else ..
  }

should work .. if you are instead using

  if ($condition == true) { ...

it might be complaining about that.

I can also recommend Perl Monks as a handy resource for Perl on the
web. It's an odd community (A W.C. Fields quote comes to mind) but has
many useful articles -- if you have a specific question that isn't
covered by any of the *many* articles on the site, ask away and you'll
have an answer before you know it.

Cheers,

Alex
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list