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

Dear Monks

I've this complex module (complex means that it is not very easy to initiate an instance of this package). Anyway, I would like to call a sub called usage, but I don't want to do this via an object. I would like to do something like
use A ; A->usage ;
However, this doesn't work... :(
Any suggestions how to do this ?

Thanks a lot
Luca

Replies are listed 'Best First'.
Re: access to sub of package
by japhy (Canon) on Feb 28, 2006 at 14:44 UTC
    The "usage" function is in the "A" namespace, therefore... A::usage(...)

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Well, that didn't work. Here is some code that demonstrates how I did it:
      package B:
      package B ; sub usage { print "test\n" ; }

      main script
      use B ; B::usage() ;
      If I run this script I get:
      Undefined subroutine &B::usage called at ./r.pl line 5. Any suggestions ?

      Luca

        The B module is part of the standard distribution and doesn't have a usage sub, so yeah you'll get an error. Use a better example module name (e.g. Foo or the like) and you'll get better results.

Re: access to sub of package
by davorg (Chancellor) on Feb 28, 2006 at 15:18 UTC

    I think that your problem is elsewhere as this works fine for me.

    # A.pm package A; sub usage { print "This is usage()\n"; } 1;
    #!/usr/bin/perl # A.pl use A; A->usage;
    $ ./A.pl This is usage()
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg