Simple use-vs-require problem

C

clarence

This has to be about the simplest question ever. And I was quite
competent with perl before I stopped using it 7 years ago. I've
been banging my head against the camel book (and the wall) for two
hours now... I'm using Perl 5.8.6

I have a module file called mod.pm, containing:
sub P {
print "in P()\n";
}
return 1;
if I do
$ perl
use mod;
P();
I get
in P()

If I do
$ perl
require mod;
mod::p();
I get
Undefined subroutine &mod::p called at - line 2.
What the heck is wrong with that?

Thanks.
 
P

Paul Lalli

This has to be about the simplest question ever. And I was quite
competent with perl before I stopped using it 7 years ago. I've
been banging my head against the camel book (and the wall) for two
hours now... I'm using Perl 5.8.6

I have a module file called mod.pm, containing:
sub P {
print "in P()\n";
}
return 1;
if I do
$ perl
use mod;
P();
I get
in P()

If I do
$ perl
require mod;
mod::p();
I get
Undefined subroutine &mod::p called at - line 2.
What the heck is wrong with that?

This has nothing to do with use vs require. Try swapping your examples
use mod;
mod::p();
will still fail, and
require mod;
P();
will still succeed.

The problem is that your mod.pm file doesn't declare itself a part of
any package. Therefore, all the code in mod.pm is in package main. A
package is not defined by the name of the file, it is defined by the
package statement.

Try putting
package mod;
as the top line of mod.pm and see what results you get now. You will
note that simply using 'use' instead of require does not grant you the
ability to call P() without qualifying it with the package name. For
that, &P has to be exported. Take a look at
perldoc Exporter
for more information

Paul Lalli
 
C

Chris Mattern

This has to be about the simplest question ever. And I was quite
competent with perl before I stopped using it 7 years ago. I've
been banging my head against the camel book (and the wall) for two
hours now... I'm using Perl 5.8.6

I have a module file called mod.pm, containing:
sub P {
print "in P()\n";
}
return 1;
if I do
$ perl
use mod;
P();
I get
in P()

If I do
$ perl
require mod;
mod::p();
I get
Undefined subroutine &mod::p called at - line 2.
What the heck is wrong with that?
Why would think that the sub "P" is in the "mod" package?
It can't be, because there IS no "mod" package; you never
defined one. You have a "mod" module, but not a "mod"
package. If you put "package mod;" at the top of mod.pm,
then all the global symbols you define in mod.pm will
then be part of package "mod". Note that now your
*first* program will break, because you haven't exported
P out of mod...
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top