http://www.perlmonks.org?node_id=315715


in reply to package Foo; isa Bar; why not?

Any chance of a require? I do like it over use base even if the only thing changing between the two is that you can say isa Bar instead of use base "Bar". There is one slight catch, though:

#!/usr/bin/perl -wl our @ISA; use isa; isa Foo,Bar; # drops Bar and also spits out warning... # works same if you quote Bar, dies if Foo # is quoted or we're under strict print "@ISA"; @ISA = (); Foo->isa("Bar"); # this works but the syntax is...funky print "@ISA"; @ISA = (); UNIVERSAL::isa(qw(Foo Bar)); # kinda long, eh? print "@ISA"; @ISA = (); main->isa(qw(Foo Bar)); # makes sense... pushes itself onto # @ISA, though __END__ Useless use of a constant in void context at isa.pl line 5. Foo Foo Bar Foo Bar main Foo Bar

It doesn't do the right thing if you'd like to declare it for more than one package at a time. With base you can do as many as you want. The only ways I can think of fixing it is to actually export a version to the package that's using isa's functionality. I dunno. Works great for one package but otherwise doesn't seem to be as clean of a solution as it could be.

Replies are listed 'Best First'.
Re: Re: package Foo; isa Bar; why not?
by liz (Monsignor) on Dec 19, 2003 at 09:37 UTC
    isa Foo,Bar; # drops Bar and also spits out warning...
                 # works same if you quote Bar, dies if Foo
                 # is quoted or we're under strict
    
    That's because it is using indirect object syntax.

    Note that you can say:

    isa Bar qw(Baz Bas Boo);
    which is not so nice as there is no way to explain why "Bar" should be specified differently from "Baz", "Bas" and "Boo". Maybe a more consistent way of specifying multiple inheritance would be:
    isa Bar, isa Baz, isa Bas, isa Boo;

    main->isa(qw(Foo Bar)); # makes sense... pushes itself onto
                            # @ISA, though
    This can be easily fixed in isa.pm:
    -push @{caller().'::ISA'}, @_; +push @{caller().'::ISA'}, map {m#^main$# ? () : $_} @_;

    ... doesn't seem to be as clean of a solution as it could be.

    I agree. I think I'll go back to just setting @ISA myself, and making sure they're run at compile time when I put several packages into one file.

    Liz

      This isn't meant as a criticism, however, why use map in place of grep?

      my $caller = caller(); push @{"$caller\::ISA"}, grep $_ ne $caller, @_;

      I do use map with the empty return quite often when a list/array requires filtering and transformation because it tends to make me feel warm and fuzzy inside. Multiple maps and greps can become confusing. However, if all you're doing is filtering then grep to be clearer (and I believe faster, too).