Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Avoiding the Use of @ISA = qw(Foo::Bar);

by hardburn (Abbot)
on Jul 14, 2003 at 17:46 UTC ( #274069=perlmeditation: print w/ replies, xml ) Need Help??

How many modules have you run across that do this?

package Foo; our @ISA = qw(Bar);

I believe that clobbering @ISA is wrong (though I'm occasionally guilty of it myself). The problem arises in that we sometimes may not know how @ISA has been modified before we clobber it. This is highly unlikely in the example above, but it could be an issue in a more complex class. Even if our current class won't give us problems, future changes could create problems.

Fortunatly, the use base pragma is just as easy to code for:

package Foo; use base 'Bar';

base will take care of munging @ISA for us, and has the additional effect of requireing the inherited package for us.

So please, leave @ISA alone. It might feel good, but it could bite you in the long run.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated

Comment on Avoiding the Use of @ISA = qw(Foo::Bar);
Select or Download Code
•Re: Avoiding the Use of @ISA = qw(Foo::Bar);
by merlyn (Sage) on Jul 14, 2003 at 18:04 UTC

      I want an extra layer of indirection between me and @ISA, because I don't like assuming that @ISA was already empty when I said our @ISA = qw(Foo);. use base provides that layer and gives you a little extra stuff.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

        Yes, but who else would set it, other than your definition of your module? You need to have one place in your program with a clear declaration of the inheritance of that class.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Avoiding the Use of @ISA = qw(Foo::Bar);
by chromatic (Archbishop) on Jul 14, 2003 at 18:07 UTC

    As Randal hints, @ISA is a package variable. If it's set somewhere else and you whack it with your code above, the other code is wrong. I'd dearly love to see an example of where this has bitten you.

    Update: If "assuming it's empty" is really a problem (if it's not empty, I'll continue to argue that there's a problem elsewhere in your code), use push or unshift, as base does. Of course, if that gets the ordering wrong, you still have problems and I don't think anything you do will solve them, short of finding out where the real problem is and fixing it.

Re: Avoiding the Use of @ISA = qw(Foo::Bar);
by dws (Chancellor) on Jul 14, 2003 at 18:14 UTC
    I'm with merlyn on this. Except in the odd case of someone writing
    package A; use base qw(B); @ISA=(C);
    there's no opportunity for @ISA to have been initialized prior to it being set explicitly. You're trying to warn people off from a demon that isn't there. Or at least I've never seen it.

    I do agree, though, that use base is a better way to go.

Re: Avoiding the Use of @ISA = qw(Foo::Bar);
by perrin (Chancellor) on Jul 14, 2003 at 19:09 UTC
    Sounds to me like you are thinking of @ISA as a "true" global, like @INC. It's actually a package variable, so @Foo::ISA is not the same as @Bar::ISA.
Re: Avoiding the Use of @ISA = qw(Foo::Bar);
by Abigail-II (Bishop) on Jul 14, 2003 at 20:29 UTC
    Eh, if I define a class, I sure don't want someone else to define what I am inheriting. If someone has set @INC I surely don't want to be beaten by that - so I set @INC.

    You know, if you start giving out advice of the form "don't use this extremely common idiom, because it might bite you", it sure helps your case by given examples or carefully explaining how it can bite you.

    Abigail

      Eh, if I define a class, I sure don't want someone else to define what I am inheriting.

      It's nice to be able to do so, though. Some classes are a pain to inherit from and adding your own class to its @INC sometimes saves a lot of time. And IMHO, it's less ugly than defining subs in the other package (and the @ISA solution adds nice ->isa() magic, which can come in handy).

      Manipulating someone else's @ISA makes fun things like http://use.perl.org/~Matts/journal/12896 possible :)

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2014-12-26 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (168 votes), past polls