Hi Monks,
I am trying to put together my first API and I am a bit stucked right now. I am just trying to get famillier with the OO side of the Perl, so I dont know whether is possible what I am trying to do or not.
So, the question is:
If I create a Kid object (see below example), which is the parent of Son and Daughter, can I call somehow the Kid's functions through the Kid, even though there is no Son instance yet? (The Kid should create one)
The reason why I am trying to do this, because I would like to be able to use the child modules separately also, plus would like to reach all the child modules from the parent.
(If I wasn't enough clear about my problem, feel free to ask me :) )
Example:
Kid.pm (parent)
------
package Kid;
sub new
{
bless ....
}
sub getSex
{
print("I am unisex \n");
}
Son.pm
------
package Kid::Son;
@ISA = qw(Kid);
use Kid;
sub getSex
{
print("I am a boy \n");
}
Daugther.pm
-----------
package Kid::Daugther;
@ISA = qw(Kid);
use Kid;
sub getSex
{
print("I am a girl \n");
}
test.pl
-------
#!/usr/bin/perl
use Kid;
use Kid::Son;
use Kid::Daugther;
# Create Instances
$kid = Kid->new();
$son = Kid::Son->new();
# 1. Its OK
$kid->getSex();
# 2. Its OK too
$son->getSex();
# 3. Its not OK
$kid->son->getSex(); # I know its wrong !
Thx, Kende
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.