Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /s/bach/a/class/cs000/public_html/pmwiki/cookbook/date.php on line 68

Warning: String offset cast occurred in /s/bach/a/class/cs000/public_html/pmwiki/cookbook/steganography.php on line 9

Deprecated: Implicit conversion from float 1260.2884615384614 to int loses precision in /s/bach/a/class/cs000/public_html/pmwiki/cookbook/steganography.php on line 9
Jack Applin | Perl / O-O

PmWiki

pmwiki.org

edit SideBar

Object-Oriented Perl

counter-test:

    #! /usr/bin/perl -w

    use strict;
    use Counter;

    my $a = new Counter;
    my $b = new Counter(42);

    print $a->next(), "\n";
    print $a->next(), "\n";

    print $b->next(), "\n";
    print $b->next(), "\n";

    print $a->next(), "\n";
    print $a->next(), "\n";

    print $b->next(), "\n";
    print $b->next(), "\n";

Counter.pm:

    package Counter;

    use strict;
    use warnings;

    sub new {
            my ($type, $value) = @_;
            $value = 1 unless defined $value;
            my $self   = { Value => $value };

            return bless $self, $type;
    }

    sub next {
            my ($self) = @_;
            return $self->{Value}++;
    }

    1; # success