19.8.4 Example

The following is the definition of an object-oriented Perl module, call this file mytest4:

package mytest4;
use strict;
use warnings;

our sub setvar
{
  my $self = shift;
  my $symbol = shift;
  my $value = shift;
  $self->{$symbol} = $value;
}

our sub getvar
{
  my $self = shift;
  my $symbol = shift;
  $self->{$symbol};
}

our sub new
{
  my ($myclass) = @_;
  my $self = {};
  bless $self, $myclass;
  $self;
}

1;

The following is a test program:

use mytest4;

use strict;
use warnings;

my $obj = mytest4->new;

$obj->setvar('xyz','abc');
print $obj->getvar('xyz');


Copyright © 2008-05-09 by Tak Auyeung