简体   繁体   English

另一个简单的Perl问题……(散列到对象数组)

[英]Another simple Perl question…(hash to an array of objects)

I'm working on creating a Graph class in Perl and I'm hoping to have each Graph contain Nodes. 我正在Perl中创建Graph类,希望每个Graph都包含Nodes。 Each Node has some properties and also (for now) an array which contains references to each other node it is connected to. 每个节点都有一些属性,并且(目前)还有一个数组,该数组包含对它所连接的每个其他节点的引用。 So far I have this definition of a node: 到目前为止,我对节点的定义如下:

use strict; 使用严格 package Node; 包节点;

sub new{
my $class = shift;
my @array = ();
my $array_r = \@array;
my $self = {
    code => undef,
name => undef,
country => undef,
continent => undef,
timezone => undef,
coordinates => undef
population => undef,
region => undef,
arrayRef => $array_r,
@_,

}; }; bless $self, $class; 祝福$ self,$ class; return $self; 返回$ self; } Yet upon calling the following function from my main script: 从我的主脚本中调用以下函数后:

sub getSetArray{
    my $self = shift;
my $param = shift;
my $temp = $self->{arrayRef};
push(@{$temp}, $param) if defined $param;
return $self->{arrayRef};
} 

and trying to iterate over a Node's array(which will contain more Nodes that it is connected to): 并尝试遍历Node的数组(它将包含与其连接的更多Node):

my $firstnode = Node->new();  # Node constructor should have @array of
my $secondnode = Node->new();

$firstnode->getSetCode("test"); 
print "The current code is ", $firstnode->getSetCode(), "\n";

my $array_r = $firstnode->getSetArray($secondnode);

$array_r = $firstnode->getSetArray($firstnode);

foreach my $obj (@{$array_r}){
    print $obj;
}

This prints out Node=HASH(0x10092bb00)Node=HASH(0x100907bd8). 这将打印出Node = HASH(0x10092bb00)Node = HASH(0x100907bd8)。 Which leads me to believe that I am dealing with an array containing 2 nodes (this is what I want). 这使我相信我正在处理包含2个节点的数组(这就是我想要的)。 But upon attempting to call any methods of this $obj's I am told that I 但是在尝试调用此$ obj的任何方法时,我被告知我

Can't call method "getSetNode" without a package or object reference. 没有包或对象引用,无法调用方法“ getSetNode”。

I had already previously blessed these two objects when calling new on these nodes. 在这些节点上调用new时,我以前已经祝福过这两个对象。 So I'm not sure why they aren't recognized as Nodes and I can't call their methods.... 所以我不确定为什么它们不能被识别为节点,并且我不能调用它们的方法。

EDIT - 编辑-

foreach my $obj (@{$array_r}){  
    print $obj->getSetCode();
}

where getSetCode() is getSetCode()在哪里

sub getSetCode{
    my $self = shift;
    my $param = shift;
    $self->{code} = $param if defined $param;
    return $self->{code};
}

Show how you are trying to call getSetNode on $obj, and the code of getSetNode? 显示您如何尝试在$ obj上调用getSetNode以及getSetNode的代码?

By the way, $temp isn't needed; 顺便说一下,不需要$ temp。 you can directly do: 您可以直接这样做:

push @{ $self->{arrayRef} }, $param if defined $param;

Maybe helpful to you: http://perlmonks.org/?node=References+quick+reference 可能对您有帮助: http : //perlmonks.org/?node=References+quick+reference

Also note that you are setting up a cyclical data structure ($firstnode indirectly referencing itself) that perl won't garbage collect automatically, even when all external references go away; 还要注意,您正在建立一个循环数据结构($ firstnode间接引用其自身),即使所有外部引用都消失了,perl也不会自动对其进行垃圾回收。 you can fix this (if this is even a concern) with: 您可以使用以下方法解决此问题(如果甚至需要解决):

if (defined $param) {
    push @{ $self->{arrayRef} }, $param;
    Scalar::Util::weaken( $self->{arrayRef}[-1] ) if $param == $self;
}

As far as your problem goes, putting all your code together (and adding a missing , on the coordinates line) into this: 就您的问题而言,将所有代码放在一起(并在坐标行上添加missing):

use warnings;
use strict;
package Node;

sub new {
    my $class = shift;
    my @array = ();
    my $array_r = \@array;
    my $self = {
        code => undef,
        name => undef,
        country => undef,
        continent => undef,
        timezone => undef,
        coordinates => undef,
        population => undef,
        region => undef,
        arrayRef => $array_r,
        @_,
    };
    bless $self, $class;
    return $self;
}

sub getSetArray{
    my $self = shift;
    my $param = shift;
    my $temp = $self->{arrayRef};
    push(@{$temp}, $param) if defined $param;
    return $self->{arrayRef};
} 

sub getSetCode{
    my $self = shift;
    my $param = shift;
    $self->{code} = $param if defined $param;
    return $self->{code};
}


my $firstnode = Node->new();  # Node constructor should have @array of
my $secondnode = Node->new();
$secondnode->getSetCode("test2");

$firstnode->getSetCode("test"); 
print "The current code is ", $firstnode->getSetCode(), "\n";

my $array_r = $firstnode->getSetArray($secondnode);

$array_r = $firstnode->getSetArray($firstnode);

foreach my $obj (@{$array_r}){  
    print $obj->getSetCode(), "\n";
}

gives this output for me: 给我这个输出:

The current code is test
test2
test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM