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

Richard Reina richard at rushlogistics.com
Mon Dec 31 08:47:26 PST 2007


Mike,

I spoke a bit too soon.  Something strange is going on -- or maybe it's not so strange.  If Fax.pm is called after require "./QuoteJoke.pl"; has already been called
                                                my $string = &main::get_quote_joke();

works like a charm and "&get_quote_joke();" fails with udefined subroutine.

If on the other hand if Fax.pm is called from a program when 

require "./QuoteJoke.pl"; has not been previously called then

 my $string = &main::get_quote_joke(); 

fails with undefined subroutine &main::get_quote_joke()

and &get_quote_joke(); works.

Any idea why this maybe the case?
                                                


Mike Fragassi <frag at ripco.com> wrote: 
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

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/chicago-talk/attachments/20071231/f4a67f64/attachment.html 


More information about the Chicago-talk mailing list