in reply to Re^2: Code style advice: where to put "use" statements?
in thread Code style advice: where to put "use" statements?

Keep in mind that the layout of any source file is supposed to be useful for the reader. The compiler or interpreter doesn't care what it looks like. I worked with a sysadmin who was vision impaired, and didn't use any indentation, anywhere, ever. That worked for him, and bash didn't care.

So here's how one of my typical scripts looks like:

#!/usr/bin/perl use strict; use warnings; # 2021-0413: An explanation of why I wrote this script. use Template; use lib '/my/local/libs'; use MyStuff; ...
Other people use different hash bang lines, perhaps using env instead. Fine. I always put strict and warnings up there, and I fix whatever errors they highlight. I also put a comment block up there -- even writing down what I think I'm doing helps clarify what my goal is. And if it might help me in six months time, great.

Then I get into the modules, and I list them in the same order I ordered my #include statements forty years ago when I was learning C -- I put the system ones first, and then my personal or local ones after that. Partly it's so that I'm reminded that my choices get higher priority than the system choices. I wrote some C code for a Perlmongers presentation on memory allocation recently, and one of the source files started like this:

#include <stdio.h> #include <assert.h> #include "mmh.h" /* This code more or less duplicates the functionality of the * original double allocation. */ struct something { int iValue; void *pvAnotherThing; };
So I've got two system includes, followed by a local include.

Anyway, work starts soon, so just to reiterate .. the source code layout is to assist the human. The computer doesn't care. I try hard to write in a style that's legible to me, and to other developers. I have pride in my work -- I want people to look at the code and have a good first impression.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.