perl:没有包或对象引用,无法调用方法“push”?

时间:2014-02-21 03:34:52

标签: perl linked-list

我被赋予了在perl中实现链表的任务,而不使用内置的push,pop,shift和unshift。这是我第一次学习perl,来自c ++和java,这就是我想出的:

#!/usr/bin/perl

sub node {
    my (@value) = @_;
    sub get {
        $next;
    }
    sub push {
        #my $next = \@_;
        if(defined($next))
        {
            $next->push(@_);
        }
        else
        {
            my $next = \@_;
        }
    }
    sub size {
        if(defined($next))
        {
            $next->size($_[0]);
        }
        else
        {
            $_[0]+1;
        }
    }
    sub myprint {
        print "$_[0]: ";
        foreach (@value) {
            print "$_, ";
        }
        print "\n";
        if(defined($next)) {
            $next->print($_[0]+1);
        }
    }
}
while(!defined($done))
{
    print "what do you want to do?\n";
    print "1 -- push\n";
    print "2 -- print nodes\n";
    print "3 -- pop\n";
    print "4 -- quit\n";
    my $val = <STDIN>;
    if ($val == 1) 
    {
        print "Type something: ";
        $input = <STDIN>;
        if(defined($top))
        {
            $top->push(node($input));
        }
        else 
        {
            $top = node($input);
        }
    }
    elsif ($val == 2)
    {
        if(defined($top))
        {
            $top->myprint(1);
        }
    }
    elsif ($val == 3)
    {
        if(defined($top))
        {
            if(defined($top->next))
            {
                $top=$top->next;
            }
        }
    }
    elsif ($val == 4)
    {
        $done=true;
    }
    else
    {
        print "Invalid option\n";
    }
}

输出:

what do you want to do?
1 --    push
2 --    print nodes
3 --    pop
4 --    quit
1
Type something: q
what do you want to do?
1 --    push
2 --    print nodes
3 --    pop
4 --    quit
1
Type something: w
Can't call method "push" without a package or object reference at ./linkedlistattempt1.pl line 76, <STDIN> line 4.

我在猜“ - &gt;”运算符只能与模块或包一起使用。

我还没有去测试其他方法,我还在努力推进。我觉得这样做的最好方法就是简单地拥有一个像子持有人{$ value = \ @_;但我不明白我将如何(可以?)添加更多变量,如下一个节点,因此子设计中的sub。所以没有perl内置函数的帮助,我该怎么做?

重要的是要提到我对可以在旧版本上运行的方法感兴趣,降至5.10。大多数(如果不是全部)教程都显示了5.16或5.18的内容

1 个答案:

答案 0 :(得分:1)

使用经典Perl OO的典型Perl实现看起来像这样。阅读手册页perlootutperlobj以了解其工作原理。

#!/usr/bin/perl
use strict;
use warnings;

package LinkedList::Node;

# Constructor.
sub new {
    my ($class, $item) = @_;

    my $self = { item => $item };

    return bless($self, $class);
}

# Read-only accessor.
sub item {
    my $self = shift;

    return $self->{item};
}

# Read-write accessor.
sub next {
    my $self = shift;

    my $next = $self->{next};
    if (@_ > 0) {
        $self->{next} = shift;
    }

    return $next;
}

package LinkedList;

# Constructor. Creates an empty linked list.
sub new {
    my $class = shift;

    return bless({}, $class);
}

# Read-only accessor.
sub head {
    my $self = shift;

    return $self->{head};
}

# Insert an item at the beginning.
sub push {
    my ($self, $item) = @_;

    my $node = LinkedList::Node->new($item);
    my $head = $self->{head};

    if ($head) {
        $node->next($head);
    }

    $self->{head} = $node;
}

package main;

my $list = LinkedList->new;

$list->push(2);
$list->push(5);
$list->push(9);

for (my $node = $list->head; $node; $node = $node->next) {
    print($node->item, "\n");
}