Trying to use my own module directory, but Perl doesn't see @EXPORT

D

David Filmer

Greetings.

I want to have my own module directory that is visible to all my Perl
programs without futzing with "use libs" and such in each program (and
I don't want to recompile Perl either). So I decided to put my module
in some version-independent directory in @INC.

So I do
perl -le 'print join "\n", @INC'
and see
...
/usr/lib/perl5/site_perl/5.8.8/i586-linux-thread-multi
/usr/lib/perl5/site_perl/5.8.8
/usr/lib/perl5/site_perl
/usr/lib/perl5/vendor_perl/5.8.8/i586-linux-thread-multi
...

Hmm. /usr/lib/perl5/site_perl sounds good. So I create a
directory /usr/lib/perl5/site_perl/Filmer and therein place a little
test module named Foo.pm:

package Foo;
BEGIN {
use Exporter ();
our @ISA = qw(Exporter);
our @EXPORT = qw{ &bar };
}
sub bar {
return "Hello World\n";
}
1;

Now I write a little test program to call Filmer::Foo::bar

#!/usr/bin/perl
use Filmer::Foo;
print bar();

And Perl complains:

Undefined subroutine &main::bar called at ./foo.pl

It doesn't matter if I
use Filmer::Foo qw{ bar };
or
print Filmer::Foo::bar();

I know that Perl is checking in the Filmer directory, because if I try
to
use Filmer::Baz; # invalid - there is no Baz.pm
then Perl (rightly) complains
Can't locate Filmer/Baz.pm in...

So why isn't Perl finding bar()?

Any insight is greatly appreciated.

Thanks!
 
G

Gunnar Hjalmarsson

David said:
Hmm. /usr/lib/perl5/site_perl sounds good. So I create a
directory /usr/lib/perl5/site_perl/Filmer and therein place a little
test module named Foo.pm:

package Foo;

Shouldn't that be

package Filmer::Foo;

?
 
B

Ben Morrow

Quoth David Filmer said:
Greetings.

I want to have my own module directory that is visible to all my Perl
programs without futzing with "use libs" and such in each program (and
I don't want to recompile Perl either). So I decided to put my module
in some version-independent directory in @INC.

You would probably be better off creating proper CPAN-style
distributions and installing them in the normal way. That way, when you
want to move the code to a new machine, you will already have all the
dependancy information. Adam Kennedy's pip (on CPAN) can be helpful when
installing distributions that aren't actually on CPAN.
Hmm. /usr/lib/perl5/site_perl sounds good. So I create a
directory /usr/lib/perl5/site_perl/Filmer and therein place a little
test module named Foo.pm:

package Foo;

You have created the file Filmer/Foo.pm but in this file you declare the
package Foo. 'use' assumes that file will declare the package
Filmer::Foo, so if you want ->import (and thus Exporter) to work, that
is what you will need to call the package.
Now I write a little test program to call Filmer::Foo::bar

#!/usr/bin/perl
use Filmer::Foo;
print bar();

And Perl complains:

Undefined subroutine &main::bar called at ./foo.pl

It doesn't matter if I
use Filmer::Foo qw{ bar };
or
print Filmer::Foo::bar();

Well, no. Neither Filmer::Foo->import nor Filmer::Foo::bar exist, so
Perl can't very well call them :).

Ben
 
J

John Bokma

David Filmer said:
Greetings.

I want to have my own module directory that is visible to all my Perl
programs without futzing with "use libs" and such in each program (and
I don't want to recompile Perl either). So I decided to put my module
in some version-independent directory in @INC.

How about setting PERL5LIB ?

PERL5LIB

A colon-separated list of directories in which to look for Perl
library files before looking in the standard library and the current
directory. If PERL5LIB is not defined, PERLLIB is used. When running taint
checks (because the script was running setuid or setgid, or the -T switch
was used), neither variable is used. The script should instead say

use lib "/my/directory";

source: http://www.perl.com/doc/manual/html/pod/perlrun.html
 
D

David Filmer

Shouldn't that be
     package Filmer::Foo;

Maybe it should be (I dunno) but it makes no difference when I try
this suggestion, except the error message becomes
 
D

David Filmer

You would probably be better off creating proper CPAN-style
distributions and installing them in the normal way.

Maybe so. Nonetheless, I think my way (which is very specific to
myself) ought to work.

You have created the file Filmer/Foo.pm but in this file you declare the
package Foo. 'use' assumes that file will declare the package
Filmer::Foo, so if you want ->import (and thus Exporter) to work, that
is what you will need to call the package.

Ben - I sense the answer to my question is here, but I cannot quite
see it. Would you kindly elaborate?
 
P

Peter J. Holzer

Maybe it should be (I dunno) but it makes no difference when I try
this suggestion, except the error message becomes

Works for me:

yoyo:~ 14:48 163% mkdir -p /usr/local/lib/site_perl/Filmer
yoyo:~ 14:49 164% cat > /usr/local/lib/site_perl/Filmer/Foo.pm
package Filmer::Foo;
BEGIN {
use Exporter ();
our @ISA = qw(Exporter);
our @EXPORT = qw{ &bar };
}
sub bar {
return "Hello World\n";
}
1;
yoyo:~ 14:49 165% cat > tmp/foo
#!/usr/bin/perl
use Filmer::Foo;
print bar();
yoyo:~ 14:50 167% chmod +x tmp/foo
yoyo:~ 14:50 168% ./tmp/foo
Hello World

hp
 
B

Ben Morrow

Quoth David Filmer said:
Ben - I sense the answer to my question is here, but I cannot quite
see it. Would you kindly elaborate?

When you write

use Filmer::Foo qw/a b c/;

what Perl actually does is

- searches @INC for the file Filmer/Foo.pm, and loads it;
- calls Filmer::Foo->import(qw/a b c/), if such a method exists.

Exporter provides you with an implementation of ->import that exports
subs according to @EXPORT, @EXPORT_OK, &c.

See also perldoc -f use, perlmod, &c.

Ben
 
D

David Filmer

Works for me:
[snip]

Well, geez, Peter, it works for me too! I dunno what I did wrong last
night.

Thanks (and also to Gunnar who proposed the solution that I messed
up).

Cheers!
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top