Search notes:

Object oriented Perl: private methods

class_A.pm

package class_A;

my $private_method = sub { #_{

#
# Declaring a private method. The method apparantly can not be called
# from outside this package.
#

  my $self = shift;
  $self->echo("private_method was called");

}; #_}

sub new { #_{

  my $class = shift;
  my $self  = {};
  bless $self, $class;
  return $self;

} #_}

sub public_method {
  my $self = shift;

  $self->echo("public_method was called, going to call private method");

  # Call private method.
  $self->$private_method();
}

sub echo { #_{
  my $self = shift;
  my $text = shift;

  print "$text\n";

} #_}


1;
Github repository about-perl, path: /object-oriented/private-methods/class_A.pm

main.pl

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

my $ca = class_A->new();
$ca->public_method();
Github repository about-perl, path: /object-oriented/private-methods/main.pl

See also

Object oriented Perl
Programming Perl: Chapter 12.5 Class Inheritance / .5 Private Methods

Index