OO linked lists in Perl
After many days, trying to implement linked lists by nested hash (link to Rosetta Code) (link to my code) or Struct::Dumb, I get how to write the (singly) linked list in object-oriented style by Perl. One with bless, another one with Moose. Keep the learning record here.
Updated: See the link in comment section of Tobyink, a showcase of his OO module Zydeco. Thanks Toby!
Updated on 2nd Aug, 2021: Add Object::Pad .
#reference: “Object Oriented Perl” by Damian Conway
use strict;
use warnings;
{
package SLL::Node;
use strict;
=pod
sub new {
my $class = $_[0];
my $objref = {
_value => $_[1],
_nextnode => $_[2],
};
bless $objref, $class;
return $objref;
}
=cut
sub new {
my ($class) = @_;
bless {
_value => $_[1],
_nextnode => $_[2],
}, $class;
}
sub value { $_[0]->{_value} }
sub nextnode { $_[0]->{_nextnode} }
}
my $node_a = SLL::Node->new(30, undef);
my $node_b = SLL::Node->new(40, \$node_a);
my $node_c = SLL::Node->new(70, \$node_b);
my $node_head = SLL::Node->new(undef, \$node_c);
my $node = $node_head;
do {
$node = ${$node->nextnode};
print $node->value, ” ”;
} while ($node->nextnode);
print ”\n”;
#print 70 40 30
use strict;
use warnings;
{
package SLL::Node;
use strict;
=pod
sub new {
my $class = $_[0];
my $objref = {
_value => $_[1],
_nextnode => $_[2],
};
bless $objref, $class;
return $objref;
}
=cut
sub new {
my ($class) = @_;
bless {
_value => $_[1],
_nextnode => $_[2],
}, $class;
}
sub value { $_[0]->{_value} }
sub nextnode { $_[0]->{_nextnode} }
}
my $node_a = SLL::Node->new(30, undef);
my $node_b = SLL::Node->new(40, \$node_a);
my $node_c = SLL::Node->new(70, \$node_b);
my $node_head = SLL::Node->new(undef, \$node_c);
my $node = $node_head;
do {
$node = ${$node->nextnode};
print $node->value, ” ”;
} while ($node->nextnode);
print ”\n”;
#print 70 40 30
#reference: various resources on Moose
use strict;
use warnings;
use strict;
use warnings;
package Node {
use Moose;
has 'value', is => 'ro', isa => 'Int';
has 'nextnode', is => 'rw', isa => 'Any';
sub printvalue {
my $self = shift;
print $self->value , " ";
}
sub go_next {
my $self = shift;
return $self->nextnode;
}
}
my $node_a = Node->new(value => 30, nextnode=> undef);
my $node_b = Node->new(value => 40, nextnode => $node_a);
my $node_c = Node->new(value => 70, nextnode => $node_b);
my $node_head = Node->new(value => 0,nextnode=> $node_c);
my $node = $node_head;
while ($node = $node->go_next ) {
$node->printvalue;
}
print "\n";
#print 70 40 30
use Moose;
has 'value', is => 'ro', isa => 'Int';
has 'nextnode', is => 'rw', isa => 'Any';
sub printvalue {
my $self = shift;
print $self->value , " ";
}
sub go_next {
my $self = shift;
return $self->nextnode;
}
}
my $node_a = Node->new(value => 30, nextnode=> undef);
my $node_b = Node->new(value => 40, nextnode => $node_a);
my $node_c = Node->new(value => 70, nextnode => $node_b);
my $node_head = Node->new(value => 0,nextnode=> $node_c);
my $node = $node_head;
while ($node = $node->go_next ) {
$node->printvalue;
}
print "\n";
#print 70 40 30
#print 70 40 30
#reference: https://metacpan.org/pod/Object::Pad
#!/usr/bin/perl
use strict;
use warnings;
use Object::Pad;
use v5.10.0;
class Node {
has $value :param;
has $nextnode :param = undef;
method get_value {
return $value;
}
method go_next {
return $nextnode;
}
}
my $node_a = Node->new(value => 30);
my $node_b = Node->new(value => 40, nextnode => $node_a);
my $node_c = Node->new(value => 70, nextnode => $node_b);
my $node_head = Node->new(value => 0, nextnode => $node_c);
my $node = $node_head;
while ($node = $node->go_next) {
print $node->get_value, " ";
}
print "\n";
#print 70 40 30
https://gist.github.com/tobyink/0dc5ac592d4a749cbf5c88699f87b07a