[Catalyst] Where to put "use"

Thomas Klausner domm at cpan.org
Thu Feb 19 07:53:18 GMT 2009


Hi!

On Wed, Feb 18, 2009 at 10:51:06AM +0000, Dermot wrote:
 
> One of my controller is starting to use a lot of other modules like
> File::Find...etc but they are usually confined to one subroutine or
> method. It's most common to see all the use statements at the top of
> the package but I have seen instances where they are declared within
> the Sub block. Is there an advantage is declaring the use of modules,
> that are only used by a single subroutine,  in that subroutine?
> 
> sub find_files : Local {
> 
> use File::Find;
> ...
> ...
> }

'use' is executed at compile time, not runtime, so as soon as Perl sees 
a 'use Foo' statement during parsing the code, it loads the module.

If you want to conditionally load a module (or only load it for some 
sub), you'll have to use 'require' (possibly followed by 'import').
Or use some of the CPAN modules helping with this.

(see this old talk from me: 
http://domm.plix.at/talks/2005_braga_using_use/index.html )

But I'd guess this would only make sense for a huge module that get's 
used very seldomly (i.e. the action in question isn't called very often)

In the end your idea sounds like premature optimisation to me :-)


-- 
#!/usr/bin/perl                              http://domm.plix.at
for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}



More information about the Catalyst mailing list