Если интересно, немного намека на ооп
| Код | #!/usr/bin/perl -w use strict;
my $list = new List;
$list->_push(1); $list->_push(2); $list->_print;
print "\n";
$list->_push( $_ ) for ( qw[ a b c d ] ); $list->_print;
package Node;
sub new { my ( $class, $value ) = @_; my $self = { value => $value, _next => undef };
bless $self, $class; } 1;
package List;
sub new { my $class = shift; my $self = { head => undef, _last => undef };
bless $self, $class; }
sub _push { my ( $self, $value ) = @_; my $node = new Node( $value );
if ( $self->{head} ) { $self->{_last}->{_next} = $node; $self->{_last} = $node; } else { $self->{head} = $node; $self->{_last} = $node; } }
sub _print { my $self = shift; my $head = $self->{head};
while ( $self->{head} ) { print $self->{head}->{value}, "\n"; $self->{head} = $self->{head}->{_next}; }
$self->{head} = $head; }
1;
|
|