Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: s/base/parent/g, base is deprecated

...

This error occurs because the BEGIN block is evaluated at the beginning of runtime, before the @ISA statement can be evaluated. Therefore, when the Derived::new() constructor is invoked, the Derived class has an empty parents list and therefore fails to invoke Base::new().

Compliant Solution (

...

parent)

This compliant solution uses the base parent module rather than directly modifying the @ISA variable.

Code Block
bgColor#ccccff
langperl
# ... package Base is unchanged

{
  package Derived;
  use baseparent qw(Base);

  sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);  # relies on established inheritence
    print "new Derived\n";
    return $self;
  };

  sub derived_value {return 2;}
}

# ... The rest of the code is unchanged

The base parent module establishes the inheritence hierarchy at parse time, before any runtime code, including the BEGIN block, is evaluated. Therefore, when the Derived::new() constructor is invoked, Perl knows that Derived is an instance of Base, and the program produces the correct output:

...

[Conway 05] pg. 360 "Inheritance"
[CPAN] Shank, Elliot, Perl-Critic-1.116 ClassHierarchies::ProhibitExplicitISA
[CPAN] DolanMaischein, ChrisMax. base parent

...