Home > Archive > PERL Beginners > July 2007 > Q: cannot use base while Exporting ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Q: cannot use base while Exporting ?
|
|
| Jack Minoshima 2007-07-18, 3:59 am |
| Hi.
Please allow me to ask another silly question.
I wrote the program and it works fine.
--------------------------------------------------------------------
Animal.pm
package Animal;
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(speak);
sub speak {
print "I speak!\n";
}
1;
----------------------------------------------------------------------
Mouse.pm
package Mouse;
use Animal;
@ISA = qw(Animal);
@EXPORT = qw(speak sound);
sub sound {
print "the sound!\n";
};
1;
------------------------------------------------------------------------
farm.pl
use Mouse;
speak;
sound;
C:\Documents and Settings\jackm\usetest>farm.pl
I speak!
the sound!
but when I change Mouse.pm to replace "use Animal and set @ISA" with use
base, it won't work.
----------------------------------------------------------------------
Mouse.pm
package Mouse;
use base qw(Animal);
@EXPORT = qw(speak sound);
sub sound {
print "the sound!\n";
};
1;
C:\Documents and Settings\jackm\usetest>farm.pl
Can't call method "sound" on an undefined value at Animal.pm line 10.
Please someone kindly let me know why?
Thank you very much in advance.
I'm using;
C:\Documents and Settings\jackm\usetest>perl -v
This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 50 registered patches, see perl -V for more detail)
Copyright 1987-2006, Larry Wall
Binary build 820 [274739] provided by ActiveState
http://www.ActiveState.com<http://www.activestate.com/>
Built Jan 23 2007 15:57:46
Perl may be copied only under the terms of either the Artistic License or
the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
wirh Windows XP SP2.
Thank you !
-- jackm
| |
| Paul Johnson 2007-07-18, 7:59 am |
| On Wed, Jul 18, 2007 at 02:20:15PM +0900, Jack Minoshima wrote:
> Hi.
> Please allow me to ask another silly question.
>
> I wrote the program and it works fine.
>
> --------------------------------------------------------------------
> Animal.pm
> package Animal;
> use Exporter;
> @ISA = qw(Exporter);
> @EXPORT = qw(speak);
> sub speak {
> print "I speak!\n";
> }
> 1;
>
> ----------------------------------------------------------------------
> Mouse.pm
> package Mouse;
> use Animal;
> @ISA = qw(Animal);
> @EXPORT = qw(speak sound);
> sub sound {
> print "the sound!\n";
> };
> 1;
>
> ------------------------------------------------------------------------
> farm.pl
> use Mouse;
> speak;
> sound;
>
> C:\Documents and Settings\jackm\usetest>farm.pl
> I speak!
> the sound!
>
> but when I change Mouse.pm to replace "use Animal and set @ISA" with use
> base, it won't work.
> ----------------------------------------------------------------------
> Mouse.pm
> package Mouse;
> use base qw(Animal);
> @EXPORT = qw(speak sound);
> sub sound {
> print "the sound!\n";
> };
> 1;
>
> C:\Documents and Settings\jackm\usetest>farm.pl
> Can't call method "sound" on an undefined value at Animal.pm line 10.
>
> Please someone kindly let me know why?
Deriving from Exporter provides your package with an import() sub that
will export the symbols you have specified (perldoc Exporter).
"use" calls a module's import sub (perldoc -f use).
"use base" doesn't (perldoc base).
The purpose of "use base" is to establish a class hierarchy. OO doesn't
really fit well with exporting. In the real world, you will probably
need to pick a single paradigm and stick to it.
--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
| |
| Paul Lalli 2007-07-19, 7:59 am |
| On Jul 18, 1:20 am, jack.minosh...@gmail.com (Jack Minoshima) wrote:
> Hi.
> Please allow me to ask another silly question.
>
> I wrote the program and it works fine.
>
> --------------------------------------------------------------------
> Animal.pm
> package Animal;
> use Exporter;
> @ISA = qw(Exporter);
> @EXPORT = qw(speak);
> sub speak {
> print "I speak!\n";}
>
> 1;
>
> ----------------------------------------------------------------------
> Mouse.pm
> package Mouse;
> use Animal;
> @ISA = qw(Animal);
> @EXPORT = qw(speak sound);
> sub sound {
> print "the sound!\n";};
>
> 1;
>
> ------------------------------------------------------------------------
> farm.pl
> use Mouse;
> speak;
> sound;
>
> C:\Documents and Settings\jackm\usetest>farm.pl
> I speak!
> the sound!
>
> but when I change Mouse.pm to replace "use Animal and set @ISA" with use
> base, it won't work.
> ----------------------------------------------------------------------
> Mouse.pm
> package Mouse;
> use base qw(Animal);
> @EXPORT = qw(speak sound);
> sub sound {
> print "the sound!\n";};
>
> 1;
>
> C:\Documents and Settings\jackm\usetest>farm.pl
> Can't call method "sound" on an undefined value at Animal.pm line 10.
>
> Please someone kindly let me know why?
> Thank you very much in advance.
> I'm using;
use base qw(Animal);
is equivalent to:
BEGIN { @ISA = qw(Animal); }
It doesn't relieve you of the necessity of using the module in the
first place. You still have to
use Animal;
Paul Lalli
| |
| Jack Minoshima 2007-07-21, 7:58 am |
| Paul and Paul,
Thank you very much !
I heard that OO module usually doesn't export anything,
but I just wanted the difference between use and use base.
Now almost everything is clear for me !
Just one thing, this code seems to work.
-------------------------------------------------------------- Animal.pm
package Animal;
use base qw(Exporter);
our @EXPORT = qw(speak);
sub speak {
print "I speak!\n";
}
1;
---------------------------------------------------------------- farm.pl
use Animal;
speak;
use base qw(Exporter) seems to work.
This means use in farm.pl calls inherited import function using qualified
name as
Animal::import
right ? (How can I check it ?)
So in summary;
- to use the module Exporter in your module, you use inheritance
- to use the module that use Exporter, you should not use inheritance
Acturelly, I want to know
why we should write
use base qw(Exporter);
but should not write just
use Exporter;
If Exporter exports its import module, then we don't have to manipulate
@ISA, right?
If so, why Export doesn't do it?
If exporting doen't fit OO inheritance, it seems to me weird that we had to
use OO inheritance functionality
to use Export module.
Thank you very much in advance !
|
|
|
|
|