David Crane David Crane - 1 month ago 8
Perl Question

What happens if I reference a package but don't use/require it?

As much as I can (mostly for clarity/documentation), I've been trying to say

use Some::Module;
use Another::Module qw( some namespaces );


in my Perl modules that use other modules.

I've been cleaning up some old code and see some places where I reference modules in my code without ever having
use
d them:

my $example = Yet::Another::Module->AFunction($data); # EXAMPLE 1
my $demo = Whats::The::Difference::Here($data); # EXAMPLE 2


So my questions are:


  1. Is there a performance impact (I'm thinking compile time) by not stating
    use x
    and simply referencing it in the code?

  2. I assume that I shouldn't
    use
    modules that aren't utilized in the code - I'm telling the compiler to compile code that is unnecessary.

  3. What's the difference between calling functions in example 1's style versus example 2's style?


Answer

You actually have a bunch of questions.

  1. Is there a performance impact (thinking compile time) by not stating use x and simply referencing it in the code?

No, there is no performance impact. But you can only use modules that have been used or required at some point. Perl keeps track of those in %INC. If you are using Some::Module but you don't have a use Some::Module in that piece of code and it works, you have the use Some::Module or require Some::Module somewhere else in one of the other modules that were loaded further up in the code.

  1. I assume that I shouldn't use modules that aren't utilized in the code - I'm telling the compiler to compile code that is unnecessary.

There is no question here. But yes, you should not do that.

It's hard to say if this is a performance impact. If you have a large Catalyst application that just runs and runs for months it doesn't really matter. Startup cost is usually not relevant in that case. But if this is a cronjob that runs every minute and processes a huge pile of data, then an additional module might well be a performance impact.

That's actually also a reason why all use and require statements should be at the top. So it's easy to find them if you need to add or remove some.

  1. What's the difference between calling functions in example 1's style versus example 2's style?

Those are for different purposes mostly.

my $example = Yet::Another::Module->AFunction($data); # EXAMPLE 1

This syntax is actually the same as this:

my $e = Yet::Another::Module::AFunction('Yet::Another::Module', $data)

It's used for class methods in OOP. The most well-known one would be new, as in Foo->new. It passes the thing in front of the -> to the function named AFunction in the package of the thing on the left (either if it's blessed, or if it's an identifier) as the first argument.

my $demo = Whats::The:Difference::Here($data);        # EXAMPLE 2

This has a syntax error. There is a : missing after The.

my $demo = Whats::The::Difference::Here($data);        # EXAMPLE 2

This is a function call. It calls the function Here in the package Whats::The::Difference and passes $data and nothing else.