Tanalis has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I have a module, Book::Reader, in which I'm attempting to call a sub.

However, calling &Book::Reader::getData causes Perl to die with an "Undefined Subroutine" error, despite the fact that it's both defined and exported.

I realise I'm probably making some stupid mistake, but it's early morning and I can't think ..

Any suggestions would be appreciated.

Cheers
--Foxcub

Replies are listed 'Best First'.
Re: Calling a sub in a Module
by Zaxo (Archbishop) on Oct 23, 2002 at 09:14 UTC

    Please show the code where the sub is defined, the package declarations, and the failing call, in the order they are encountered. Otherwise you're asking us to just guess.

    After Compline,
    Zaxo

      # Code where sub is defined: use Exporter(); my @ISA = qw/Exporter/; my @EXPORT = qw/&getData/; sub getData { # do stuff # return a hash } # Package declarations - none, that I'm aware of. Never # had this problem before, using &Reader::getData and the # like. # Failing call: use Book::Reader; my $hashref = &Book::Reader::getData;
        Package declarations - none
        You need a package declaration of Book::Reader otherwise getData() will live in the main package. Also if you want to export methods in the current package you don't need to put an ampersand (&) before the name1, and @ISA and @EXPORT need to be package level variables e.g
        package Book::Reader; require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( getData ); use strict; sub getData { ... }

        HTH

        _________
        broquaint

        1 as chromatic notes in his node Exporter will treat &sub the same as sub in the @EXPORT list

        Two things: if that is exactly what your code says, then you're missing a package declaration. Also you can't use my there. Should be our

        package Book::Reader; use strict; use Exporter; our @ISA = qw/ Exporter /; our @EXPORT = qw/ getData /; ....

        Incidentally, the Expoter bit doesn't mean anything if you are calling your sub with a fully qualified name like Book::Reader::getData(), so if you don't need to export it, you should get rid of those lines...

Re: Calling a sub in a Module
by broquaint (Abbot) on Oct 23, 2002 at 09:15 UTC
    Are you positive that it's defined? Does defined &Book::Reader::getdata return true? Lastly are you sure you've imported the module into your program?
    HTH

    _________
    broquaint