User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

perl:pool-machine [2020-02-15 00:57] (current)
Line 1: Line 1:
 +<code perl logspool.pl> ​
 +#​!/​usr/​bin/​env perl
 +
 +package Pool;
 +
 +use strict;
 +use warnings;
 +
 +sub new {
 +    my $class = shift;
 +    my @pool;
 +    my $self = {
 +        pool => \@pool,
 +        pos => 0
 +    };
 +    bless $self, $class;
 +    return $self;
 +}
 +
 +sub pool {
 +    my ($self, $pool) = @_;
 +    return $self->​{pool} unless $pool;
 +    $self->​{pool} = $pool;
 +}
 +
 +sub pos {
 +    my ($self, $pos) = @_;
 +    return $self->​{pos} unless $pos;
 +    return undef if $pos > ($self->​count + 1);
 +    $self->​{pos} = $pos;
 +}
 +
 +sub add {
 +    my ($self, $elem) = @_;
 +    return undef unless $elem;
 +    push @{$self->​{pool}},​ $elem;
 +    $self;
 +}
 +
 +sub count {
 +    my $self = shift;
 +    scalar @{$self->​{pool}};​
 +}
 +
 +sub get {
 +    my ($self, $pos) = @_;
 +    $pos = $self->​{pos} unless $pos;
 +    return undef if $pos < 1 or $pos > $self->​count;​
 +    @{$self->​{pool}}[$pos - 1] || undef;
 +}
 +
 +sub next {
 +    my $self = shift;
 +    return undef if ($self->​pos + 1) > $self->​count;​
 +    $self->​pos($self->​pos + 1);
 +    $self->​get($self->​pos);​
 +}
 +
 +sub clear {
 +    my $self = shift;
 +    my @pool;
 +    $self->​{pool} = \@pool;
 +    $self;
 +}
 +
 +
 +1;
 +
 +use strict;
 +use warnings;
 +use Mojo::Util qw(dumper);
 +
 +my $p = Pool->​new;​
 +#​$p->​add('​abc'​);​
 +#​$p->​add('​def'​);​
 +#​$p->​add('​qwe'​);​
 +
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print "​-----------\n";​
 +#
 +#​$p->​add('​opl'​);​
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#​$p->​add('​ASD1'​);​
 +#​$p->​add('​ASD2'​);​
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​next;​
 +#print dumper $p->pos;
 +#
 +#print dumper $p->​get(3);​
 +#print dumper $p->​get(7);​
 +
 +
 +
 +#​$p->​pos(0);​
 +#print dumper($p->​get);​
 +#​$p->​pos(1);​
 +#print dumper($p->​get);​
 +
 +#​$p->​pos(2);​
 +#print dumper($p->​get);​
 +
 +#​$p->​pos(3);​
 +#print dumper($p->​get);​
 +
 +#print dumper($p->​get(1));​
 +
 +#​$p->​clear;​
 +
 +#​$p->​pos(1);​
 +#print dumper($p->​get);​
 +
 +open(my $fh, '<',​ "​README"​);​
 +while (my $line = readline($fh)) {
 +    $p->​add($line);​
 +}
 +
 +while (my $line = $p->​next) {
 +    print $line;
 +}
 +
 +#EOF
 +</​code>​
 +
 +----
 +[<>]
 +