Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Learning how to use inheritance...

by kiat (Vicar)
on Oct 07, 2003 at 15:32 UTC ( #297315=perlquestion: print w/ replies, xml ) Need Help??
kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I found some oo perl example code to show how inheritance works. The following is the code in its originality:
#!/usr/local/bin/perl -w package One; sub new { $class = shift; print "creating object of class $class\n"; bless {}, $class; } package Two; @ISA = ("One"); sub new { $class = shift; print "creating object of class $class\n"; bless {}, $class; } $one = One->new(); $two = Two->new();
Since there are two package names One and Two (corresponding to two classes), I'll need to save 'package One' as One.pm and 'package Two' as 'Two.pm'.

My question is, shouldn't there be a third file (one that's named say main.pl ?) that is being called when one executes the program? In order words, do I need to create a third file (shown below) in addition to the two pm files:
# main.pl use One; use Two; $one = One->new(); $two = Two->new(); # So, to execute the program, I type >perl main.pl
Another question I've is: What difference does '@ISA = ("One");' in Two.pm make? I mean there's still a need to declare 'use One' in main.pl before it will execute. When I comment out '@ISA = ("One");' in Two.pm, the code still runs.

I'm new to perl oo so please forgive my rather elementary questions ;)

Comment on Learning how to use inheritance...
Select or Download Code
Re: Learning how to use inheritance...
by broquaint (Abbot) on Oct 07, 2003 at 15:46 UTC
    My question is, shouldn't there be a third file (one that's named say main.pl ?
    For the purposes of demonstration, seperating the packages into seperate files isn't necessary, or even seperating the 'main' code for that matter. However, if you want to use the packages One and Two you will need to save them as a .pm files in the library path. As for the 'main' section of code, that could be saved to a file, or piped into STDIN or even in as commandline argument to -e. As ever TIMTOWTDI :)
    What difference does '@ISA = ("One");' in Two.pm make?
    It means that any subroutines in One are inherited by Two (which wasn't demonstrated in your example) e.g
    { package One; sub new { bless [], shift } sub onefunc { print "$_[0] in One::onefunc" } package Two; @ISA = 'One'; } my $two = Two->new; $two->onefunc(); __output__ Two=ARRAY(0x80fba1c) in One::onefunc
    So as you can see from the output Two has inherited One's methods i.e it has access to them (but not vice versa, hence the word 'inheritence'). For more information on inheritence see. perlboot and perltoot.
    HTH

    _________
    broquaint

Re: Learning how to use inheritance...
by Abigail-II (Bishop) on Oct 07, 2003 at 15:47 UTC
    Everything could be in one file, using two or three files don't magically do something other than creating lexical scopes.

    As for the assignment to @ISA, you are really assigning to @Two::ISA, and you are telling Perl that the class Two inherits the class One. A use statement doesn't set up any inheritance relationship - it just includes code (and run import() if there is one, but that's not relevant in this case).

    The fact that the code still runs if you omit the @ISA assignment is that you aren't using the inheritance. The class One has just one method: new, but that method is also defined in class Two. So, One has nothing that Two can delegate to.

    Abigail

Re: Learning how to use inheritance...
by CombatSquirrel (Hermit) on Oct 07, 2003 at 15:48 UTC
    Actually, you don't even need two files. The "real" program is just in a package called "main" (packages are largely file-independent). Try the following:
    #!perl -w package One; sub new { my $class = shift; print "creating object of class $class\n"; bless {}, $class; } package Two; @ISA = ("One"); package main; my $one = One->new(); my $two = Two->new();
    Your code in two.pm still runs after removing the @ISA array, because you are overriding the inheritance of the new method by redeclaring it in the second package. Just remove the @ISA part in the above code and you will see where it leads you ;-).
    Hope this helped.
    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: Learning how to use inheritance...
by fglock (Vicar) on Oct 07, 2003 at 18:07 UTC

    "packages" organize the code into namespace units. This is what it looks without them:

    sub One::new { my $class = shift; print "creating object of class $class\n"; bless {}, $class; } @Two::ISA = ("One"); my $one = One->new(); my $two = Two->new(); # creating object of class One # creating object of class Two
Re: Learning how to use inheritance...
by kiat (Vicar) on Oct 08, 2003 at 09:31 UTC
    Thanks to all for helping with my perl oo and inheritance questions :)

    Let's say I choose the packages-in-different-files approach, is the following code correct (3 files, the first is named One.pm, the second Two.pm and the third interface.pl)?
    # One.pm package One; sub new { my $class = shift; print "creating object of class $class\n"; bless {}, $class; } 1; # Two.pm package Two; @ISA = 'One'; 1; # interface.pl use One; use Two; my $one = One->new(); my $two = Two->new();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://297315]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-12-21 13:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (105 votes), past polls