[Chicago-talk] accessing another script's subroutine from w/in a module?

Mike Fragassi frag at ripco.com
Sat Dec 29 21:21:01 PST 2007


On Fri, 28 Dec 2007, Richard Reina wrote:

> How can one properly declare another script's subroutine from within a 
> Module.
>
> I would like to be able to do this:
>
> require "./QuoteJoke.pl";
> my $string = &get_quote_joke();
>
> from a my home made perl module Fax.pm
>
> without getting:
>
> Undefined subroutine &Fax::get_quote_joke called at Fax.pm line 61

Inside Fax.pm, assuming that this module has the line 'package Fax;', 
then calling any method 'foo' will be interpreted as a call to 
&Fax::foo.

If a file (.pl or .pm) does not have a 'package' declaration, its 
commands are in the package 'main' by default.  If that's the case with 
QuoteJoke.pl, then inside of Fax.pm, calling '&main::get_quote_joke()' 
should work.  Otherwise, if there's a package, use that instead of 
'main'.

> I know I am probably breaking some cardinal rule of perl but I can't 
> figure it out.

You're not really breaking anything, or doing anything crazy; you just 
need to prepend the package name where the method comes from, when 
you're calling it from inside a different package.

-- Mike F.


More information about the Chicago-talk mailing list