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

Discussion in 'Perl Misc' started by David Filmer, Aug 4, 2008.

  1. David Filmer

    David Filmer Guest

    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!

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    David Filmer, Aug 4, 2008
    #1
    1. Advertising

  2. David Filmer wrote:
    > 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;

    ?

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


    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 4, 2008
    #2
    1. Advertising

  3. David Filmer

    Ben Morrow Guest

    Quoth 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.


    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

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
    Ben Morrow, Aug 4, 2008
    #3
  4. David Filmer

    John Bokma Guest

    David Filmer <> wrote:

    > 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

    --
    John

    http://johnbokma.com/perl/
    John Bokma, Aug 4, 2008
    #4
  5. David Filmer

    David Filmer Guest

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

    On Aug 3, 6:27 pm, Gunnar Hjalmarsson <> wrote:
    > 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

    >>> Undefined subroutine &Filmer::Foo::bar called at ...


    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    David Filmer, Aug 4, 2008
    #5
  6. David Filmer

    David Filmer Guest

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

    On Aug 3, 6:29 pm, Ben Morrow <> wrote:
    > 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?

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    David Filmer, Aug 4, 2008
    #6
  7. Re: Trying to use my own module directory, but Perl doesn't see@EXPORT

    On 2008-08-04 04:35, David Filmer <> wrote:
    > On Aug 3, 6:27 pm, Gunnar Hjalmarsson <> wrote:
    >> 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
    >
    >>>> Undefined subroutine &Filmer::Foo::bar called at ...


    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
    Peter J. Holzer, Aug 4, 2008
    #7
  8. David Filmer

    Ben Morrow Guest

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

    Quoth David Filmer <>:
    > On Aug 3, 6:29 pm, Ben Morrow <> wrote:
    >
    > > 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?


    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

    --
    For the last month, a large number of PSNs in the Arpa[Inter-]net have been
    reporting symptoms of congestion ... These reports have been accompanied by an
    increasing number of user complaints ... As of June,... the Arpanet contained
    47 nodes and 63 links. [ftp://rtfm.mit.edu/pub/arpaprob.txt] *
    Ben Morrow, Aug 4, 2008
    #8
  9. David Filmer

    David Filmer Guest

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

    On Aug 4, 5:52 am, "Peter J. Holzer" <> wrote:

    > 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!

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    David Filmer, Aug 5, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saverio M.
    Replies:
    0
    Views:
    501
    Saverio M.
    Jul 3, 2006
  2. Jan Drugowitsch
    Replies:
    1
    Views:
    285
    Diez B. Roggisch
    Mar 7, 2004
  3. Steve Kershaw
    Replies:
    1
    Views:
    343
    Brennan Stehling
    Sep 26, 2006
  4. process
    Replies:
    1
    Views:
    1,178
    Robert Kern
    Oct 17, 2008
  5. Justin Wyllie
    Replies:
    5
    Views:
    233
    Peter Pan
    Jan 11, 2005
Loading...

Share This Page