Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by Bod (Parson)
on Apr 13, 2021 at 22:12 UTC ( [id://11131227]=note: print w/replies, xml ) Need Help??


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

But if there's a piece of Perl code that needs to run as quickly as possible, I'd probably re-consider that approach

Given that use happens at compile time, why would the location of the statement in the code make any difference to the speed of the code?
*scratches head*

Replies are listed 'Best First'.
Re^3: Code style advice: where to put "use" statements?
by talexb (Chancellor) on Apr 14, 2021 at 13:26 UTC
      .. why would the location of the statement in the code make any difference to the speed of the code?

    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.

Re^3: Code style advice: where to put "use" statements?
by eyepopslikeamosquito (Archbishop) on Apr 14, 2021 at 06:12 UTC

    Nice necropost. :)

    talexb didn't say he'd use use, he just said he'd reconsider that approach. I assume he'd use require inside a sub (possibly calling the package's import method also). If talexb is listening, he'll notice your necropost, and we'll see if he can outdo Marshall and remember what the hell he was thinking thirteen years ago. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131227]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found