Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Perl - Modules)  »  trickiness (?) calling regular subroutines from OO methods
Page 1 of 1    

trickiness (?) calling regular subroutines from OO methods

Author Message
Ian Giblin
Posted: Mon Aug 23, 2004 9:16 pm
Guest
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.
 
Brian McCauley
Posted: Mon Aug 23, 2004 9:32 pm
Guest
Quote:
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.
 
Ian Giblin
Posted: Mon Sep 13, 2004 6:53 am
Guest
Brian McCauley <nobull@mail.com> wrote in message news:<cgd9ge$s1r$1@sun3.bham.ac.uk>...
Quote:

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.
 
 
Page 1 of 1    
All times are GMT
The time now is Mon Nov 23, 2009 10:44 am