tradez has asked for the wisdom of the Perl Monks concerning the following question:

I am coming to the monistary gates to prove a point in an arguement between myself an a fellow programmer. I am trying to figure out the importants of using the @ISA array for showing inheritance, but I have not used it very in depth and cannot find any sources that explain its uses. Also, are we looking at any issues, other than poor naming structure, by using a scalar value $ISA? I am assuming not. Please bless me with your wisdom oh mighty perl monks.

Replies are listed 'Best First'.
(Ovid) Re: A question of Inheritance
by Ovid (Cardinal) on Dec 12, 2001 at 00:39 UTC

    The @ISA array is used in a package to tell Perl where to find a method if that method is not defined in said package. This is known as "inheritance". If I want to subclass Foo as Foo::Bar, I would do this:

    package Foo::Bar; use strict; use Foo; # don't forget this line! use vars qw/ @ISA /; @ISA = qw/ Foo /;

    That sets Foo as a base class of Foo::Bar. Further, if Foo has a base classes, Perl will search through them for a method call to a Foo::Bar object (or class method) in order to find something useful.

    I can't think of any problems that using a scalar named $ISA would cause, but you are right that it could be bad form.

    Now, since your question was "Void for Vagueness" :), I have to ask: did I answer it?

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Just to be nit-picky, this is what I tend to call "method inheritance" (aka "interface inheritance"). As opposed to "instance attribute inheritance" and "class attribute inheritance". @ISA will not coordinate the underlying data structures for you, nor will it do anything for package- or file-scoped variables.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Just to nit-pick your nit-picking :), is that a limitation of @ISA or a limitation of Perl? If you wanted base classes to "inherit" class attributes, don't you pretty much need to have the base class export the data or inherit methods that will provide said data?

        I'd appreciate more discussion on this because Perl was my first exposure to writing OO code and now that I've been studying Java, I'm really beginning to see some of the limitations that Perl has in this area. In particular, I'd really appreciate being able to provide true method overloading, but I suspect Perl's variadic functions (and, perhaps, not being able to use prototypes with methods) are the reason this wasn't implemented, short of a generic method that uses an internal dispatch table based upon the contents of @_. Hmm... maybe I should add this to my XMas wish list :)

        Now there's an interesting thought for a meditation... how do Monks simulate missing OO features in Perl?

        • Method overloading
          Class::Multimethods

        • Data inheritence
          Class::Class? (unless I misread the docs)

        • Separate class and instance methods
          Bless the object into a separate package?

        • Others?

        Cheers,
        Ovid

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Ovid, that was actually perfect. I knew I could count on the perlmonks for an answer!
Re: A question of Inheritance
by VSarkiss (Monsignor) on Dec 12, 2001 at 00:53 UTC
      Don't forget perlboot -- merlyn's beginners OO tutorial.

      It is an excellent document and a great way to start learning OO. I'd recommend reading this one first.

      -Blake

Re: A question of Inheritance
by strat (Canon) on Dec 12, 2001 at 18:09 UTC
    I would propably use $ISA (or according to my programming style: $isa) in a construction like the following:
    foreach my $ISA (@ISA){ # anything } # foreach
    But I think i might have never done so yet...

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"