オブジェクト指向を勉強してみる2

package Foo;

use strict;
use warnings;

sub new
{
        my $class = shift;
        my $self = {
                piyo => 1,
                cnt  => 0,
        };
        bless $self, $class;
        return $self;
}

sub bar
{
        my $self = shift;
        $self->{cnt}++;
        return $self->{cnt};
}

package main;

use strict;
use warnings;
use Data::Dumper;

my $foo = Foo->new();
print Dumper($foo);

foreach (1..3) {
        print $foo->bar() . "\n";
}

結果:
$VAR1 = bless( {
                 'piyo' => 1,
                 'cnt' => 0
               }, 'Foo' );
1
2
3