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

thoglette has asked for the wisdom of the Perl Monks concerning the following question: (object-oriented programming)

By default (5.004_02) adding
use strict;
causes the usual inheritance line of
@ISA = ("InheritedClass");
to fail for not being fully declared.
Trying local and my don't work - when running Perl complains Can't locate object method "new" via package

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I use @ISA and use strict at the same time?
by Vynce (Friar) on Jun 05, 2001 at 13:45 UTC

    TIMTOWTDI:

    use strict; package Question::Loaded; use vars ('@ISA'); @ISA = ('Question');
    or
    package Question::Open; @ISA = ('Question'); use strict;
    or
    package Question::MillionDollar; use strict; our @ISA = ('Question'); # if (perl -v >= 5.6)
    and there are other ways, too. it's a matter of style.

    Edit by tye: Note that use strict can appear before the package line but that putting use vars before the package line will cause the file to fail to compile while putting our before the package line will not work in less obvious ways.

Re: How do I use @ISA and use strict at the same time?
by davorg (Chancellor) on Jun 05, 2001 at 12:48 UTC
    use vars qw(@ISA);
Re: How do I use @ISA and use strict at the same time?
by merlyn (Sage) on Jun 05, 2001 at 18:38 UTC
    If the parent class is in a separate file, consider using base.
Re: How do I use @ISA and use strict at the same time?
by Anonymous Monk on Jun 05, 2001 at 18:05 UTC
    I think the best is
    package Question; @Question::ISA=("Another");
      Another way is to use vars @ISA, this makes the package a little more portable inside of a source tree.
      package Question;
      
      use strict;
      use vars qw( @ISA );
      
      @ISA = ( "Another" );
Re: How do I use @ISA and use strict at the same time?
by cpousset (Initiate) on Jul 12, 2001 at 18:53 UTC
    No Problem!
    use strict; package InheritedClass; # ... stuff for InheritedClass package NewClass; @NewClass::ISA = ("InheritedClass");
    Strict was kvetching about @ISA, which is actually a class variable within NewClass.

    Originally posted as a Categorized Answer.

Re: How do I use @ISA and use strict at the same time?
by Narveson (Chaplain) on Jan 23, 2008 at 16:49 UTC
    use strict; package Wombat; use base 'Marsupial';
    See Conway, Perl Best Practices, chapter 16: Class Hierarchies, Inheritance.