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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759406208, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/object-oriented/private-methods(98): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78