« Wikipedia Moment | Main | Rhapsody Updates »

Perl Module Use

Avoid plain use Module::Name in favor of either an empty import list, or a specific list of items to trample on the local namespace with:

use CGI; # expensive! use CGI (); # much better use POSIX; # slow and much trampling use POSIX qw(strftime); # just this subroutine

Limiting module imports saves time, and lessens the risk that two functions with the same name will be used. A quick review of the use statements will generally reveal exactly what has been imported, such as strftime in the example above. Should a new module version export more by default, the explicit import list will avoid any namespace changes to scripts.

Technorati Tags:

Benchmarking use properly is tricky…

#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); cmpthese( -5, { 'use-import' => sub { eval "use CGI;" }, 'use-empty' => sub { eval "use CGI ();" }, });