Search notes:

Object oriented Perl

The base class: Base.pm

package Base;
use warnings;
use strict;

sub new { #_{

  my $package = shift;
  my $data    = shift;

  print "Base::new:    package=$package, data=$data\n";

  my $self = {};
  $self->{data} = $data;

  bless $self, $package;

} #_}

sub method { #_{

  my $self = shift;

  printf "Base::method, ref(\$self)=%s, data=%s\n", ref($self), $self->{data} ;

} #_}

sub overridden { #_{

  my $self     = shift;
  my $argument = shift;

  printf "Base::overridden,    ref(\$self)=%s, argument=%s\n", ref($self), $argument;

} #_}

sub static_ { #_{

#
# No need for a $self argument in
# static methods
#

  my $argument = shift;
  printf "Base::static_        argument=$argument\n";

} #_}

1;
Github repository about-perl, path: /object-oriented/simple/Base.pm

The derived class: Derived.pm

package Derived;
use warnings;
use strict;

our @ISA = qw(Base);

sub new { #_{

  my $package = shift;
  my $data    = shift;
  print "Derived::new: package=$package, text=$data\n";

  return $package->SUPER::new($data);

} #_}

sub overridden { #_{

  my $self     = shift;
  my $argument = shift;

  printf "Derived::overridden, ref(\$self)=%s, argument=%s\n", ref($self), $argument;

  $self->SUPER::overridden($argument);

} #_}

1;
Github repository about-perl, path: /object-oriented/simple/Derived.pm

main.pl

#!/usr/bin/perl
use warnings;
use strict;

use Base;
use Derived;

my $base = Base->new('B');

print "\n-----------------\n";

$base -> method;
$base -> overridden('b');

print "\n-----------------\n";

my $derived = Derived->new('D');

print "\n-----------------\n";

$derived -> method;

print "\n-----------------\n";

$derived -> overridden('d');

print "\n-----------------\n";

#
# Note: call the static method with preceding
# double colons, not with ->
#
Base::static_('S');
Github repository about-perl, path: /object-oriented/simple/main.pl

See also

isa, SUPER, private methods
Perl module Object::InsideOut
Perl

Index