Nosrettap
Nosrettap

Reputation: 11320

Why do I need a use statement in this perl program?

I have a perl subroutine called foo that is in file C.pm. C.pm is in directory B and that is in directory A as follows: A > B > C.pm > foo

I'm trying to call the foo subroutine from another file. If I do the following it works:

use A::B::C qw(foo);

//Code here

foo($temp)

However, the following doesn't work

//Code here

A::B::C::foo($temp)

Why not? I thought I didn't need to include the use statement if I explicitly wrote out the path when calling that subroutine.

Upvotes: 1

Views: 132

Answers (1)

amon
amon

Reputation: 57590

The use does two things:

  1. It requires a file, which parses, compiles and executes it
  2. It calls the import method on the new module, which can install subroutines in your current namespace.

You have to somehow execute a module before using subs defined in it.

If you don't want to import any subroutines or other symbols, you can give use the empty list:

use A::B::C ();

Upvotes: 9

Related Questions