Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple use-vs-require problem

Reply
Thread Tools

Simple use-vs-require problem

 
 
clarence@silcom.com
Guest
Posts: n/a
 
      01-25-2005
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:();
I get
Undefined subroutine &mod: called at - line 2.
What the heck is wrong with that?

Thanks.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-25-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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:();
> I get
> Undefined subroutine &mod: 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:();
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

 
Reply With Quote
 
 
 
 
Chris Mattern
Guest
Posts: n/a
 
      01-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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:();
> I get
> Undefined subroutine &mod: 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?"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple VB.Net / webservice requirement (but not simple for me....) Dave E ASP .Net 7 01-11-2006 02:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM
Simple problem with a simple chat script dillon Javascript 1 11-17-2003 11:11 PM



Advertisments