Click here to get back home

trickiness (?) calling regular subroutines from OO methods

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
trickiness (?) calling regular subroutines from OO methods Ian Giblin 08-23-2004
Posted by Ian Giblin on August 23, 2004, 11:16 am
Please log in for more thread options
When I started to learn, I first wrote a lot of non-object-oriented
perl. This included a function called read_file (let's say it just
dumps a file to screen), which I put in a module called Support.pm, in
a subdirectory called Ian. I used this function successfully.

Now I'm writing OO perl and I am having trouble calling the subroutine
from inside a method. For example:

In the "main" function, oo_test.pl:

use strict;
use Ian::Support;
read_file("A.txt");

....works fine, but if it's in a package like this:

use strict;
use Ian::Support
package Ian::File
....
sub read
{
...
read_file("A.txt");
}

....then, when invoked from the main program, the method fails with the
error "Undefined Subroutine Ian::File::read_file in <package File.pm>
at line <whatever>".

Due to the steep learning curve I think I might have missed something
important. I am learning from all the usual books including Conway's
great "Object Oriented Perl" but I haven't got it with me at the
moment.

Thanks in advance if you can help,
Ian.


Posted by Brian McCauley on August 23, 2004, 7:32 pm
Please log in for more thread options
> use strict;
> use Ian::Support;
> read_file("A.txt");
>
> ...works fine, but if it's in a package like this:
>
> use strict;
> use Ian::Support
> package Ian::File
> ...
> sub read
> {
> ...
> read_file("A.txt");
> }
>
> ...then, when invoked from the main program, the method fails with the
> error "Undefined Subroutine Ian::File::read_file in <package File.pm>
> at line <whatever>".

When you use an Exporter module it exports functions into the namespace
of currently selected package. It does not export functions into the
namespace of the package that will become current after the next line.

If you want to import into several package then put a use after each
package directive.

A module should never have any use directives[1] (other than pragmas
like strict and warnings) before the first package directive as it is
undefined where the imported stuff will go.

[1] Unless you use the special use syntax that explicitly supresses import.



Posted by Ian Giblin on September 12, 2004, 8:53 pm
Please log in for more thread options
>
> When you use an Exporter module it exports functions into the namespace
> of currently selected package. It does not export functions into the
> namespace of the package that will become current after the next line.
>
> If you want to import into several package then put a use after each
> package directive.
>
> A module should never have any use directives[1] (other than pragmas
> like strict and warnings) before the first package directive as it is
> undefined where the imported stuff will go.
>
> [1] Unless you use the special use syntax that explicitly supresses import.

First, thanks for the reply. It took me a few reads to realise that
the way I was originally using modules was naive - I wasn't using an
exporter module and I was not naming them using the "Package ..."
directive. So, they were always named the same as their filenames.
Then when I started trying to write better code, also in OO form, I
actually defined the package names and ran into problems. Your post
was aimed at a more experienced programmer than I was [then], but it
explained a lot to me.

Best Regards,
Ian.


Similar ThreadsPosted
How do I keep subroutines Local to the libraries I am calling? October 16, 2006, 11:05 am
calling methods on deserialized objects August 25, 2006, 4:32 pm
Exporting dependency methods November 10, 2004, 8:14 am
How to parse with regular expression... May 5, 2005, 6:33 am
Regular Expression Generator June 26, 2006, 1:42 am
Simple Regular Expression Help May 16, 2007, 1:59 pm
Log::Dispatch - How to "die" a script after all other methods are called??? September 6, 2005, 4:55 pm
regular expression problem ? and * characters May 28, 2006, 7:02 am
Passing Environment and ExtUtils::MakeMaker options to CPAN methods August 5, 2005, 8:42 am
Calling C++ / MFC / SDKs usinf Inline July 16, 2005, 12:36 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap