Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do I use @ISA and use strict at the same time?

( #85738=categorized question: print w/ replies, xml ) Need Help??
Contributed by thoglette on Jun 05, 2001 at 12:45 UTC
Q&A  > object-oriented programming


Description:

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

Answer: How do I use @ISA and use strict at the same time?
contributed by davorg

use vars qw(@ISA);

Answer: How do I use @ISA and use strict at the same time?
contributed by Vynce

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.

Answer: How do I use @ISA and use strict at the same time?
contributed by merlyn

If the parent class is in a separate file, consider using base.

Answer: How do I use @ISA and use strict at the same time?
contributed by Anonymous Monk

I think the best is

package Question; @Question::ISA=("Another");
Answer: How do I use @ISA and use strict at the same time?
contributed by Narveson

use strict; package Wombat; use base 'Marsupial';
See Conway, Perl Best Practices, chapter 16: Class Hierarchies, Inheritance.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2014-09-22 05:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (179 votes), past polls