Optional Perl Module Loading
Since the following information is not spelled out between the use, require, and import documentation. The following example only loads the CGI module if it is available, then pollutes the current namespace with header. Somewhat equivalent to use CGI qw/header/, but different.
#!/usr/bin/perl -w use strict; eval { require CGI; }; if (! $@) { CGI->import( qw/header/ ); } print header();
The above should always work, as CGI has shipped with Perl for quite some time. Use the same pattern for other modules:
#!/usr/bin/perl eval { require Win32; }; if (! $@) { Win32->import( qw/SW_SHOWNORMAL/ ); }
If possible, skip the import, and use object oriented access methods to minimize namespace pollution. To see how use differs from require, try placing the use statements at the end of a file, or inside an END block.
#!/usr/bin/perl print header(); END { use CGI qw(header) }
Technorati Tags: Perl