Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does "use ...." at the top add overhead to a Perl script?

I've always wondered this. I have a habit of always adding

use strict;
use warnings;
use Data::Dumper;

to the top of every script I write. Does this add overhead if I don't even use the Dumper function? Also, in most cases Data::Dumper was called earlier in another package but I need it in this package so I include it again. In this case does it produce additional overhead?

like image 389
Ryan Detzel Avatar asked Jun 12 '09 14:06

Ryan Detzel


2 Answers

If they have BEGIN blocks or import routines, then yes, it always adds overhead. Also any mainline code will be executed eventually and any INIT, CHECK, and END blocks will also execute.

The only way it won't add overhead is if the module expects use to be nothing more than like require. (Of course, require also runs everything except the import routine, but that's why I mentioned the view from the use-d module. It "expects" to be nothing but a simple require.)

If you want to retain that line, for some reason, just comment it out. In development, it's okay to have modules that you don't use. In QA or production, that's a mistake, IMO.

like image 114
Axeman Avatar answered Nov 04 '22 12:11

Axeman


Perl must parse the code in Dumper.pm, so your program will be slower to start up. This is normally a very trivial hit to performance. Also, any code not in functions or in the import function will run. This may have a minor impact on your start up time. You will also consume more memory (the AST for the code and any data structures the code builds). It isn't the best thing you can do, but it is far from the worst. Unless your programs very often (multiple times a minute), you should not notice any real improvement in speed by removing the line.

like image 32
Chas. Owens Avatar answered Nov 04 '22 12:11

Chas. Owens