in reply to Calling a sub in a Module

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

Replies are listed 'Best First'.
Re: Re: Calling a sub in a Module
by Tanalis (Curate) on Oct 23, 2002 at 09:29 UTC
    # 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

        Also if you want to export methods in the current package you don't put an ampersand (&) before the name

        Are you sure? Exporter would seem to disagree.

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

        Cool, that's soted it. Cheers for the help. :)