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

richard at rushlogistics.com richard at rushlogistics.com
Sun Dec 30 14:52:59 PST 2007


Mike,

Your suggestion worked. Thank you for your very instructive response. I owe you a beer!

Have a great new year!

Richard
Sent via BlackBerry from T-Mobile

-----Original Message-----
From: Mike Fragassi <frag at ripco.com>

Date: Sat, 29 Dec 2007 23:21:01 
To:"Chicago.pm chatter" <chicago-talk at pm.org>
Subject: Re: [Chicago-talk] accessing another script's subroutine from w/in
 a module?



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.
_______________________________________________
Chicago-talk mailing list
Chicago-talk at pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk


More information about the Chicago-talk mailing list