难道perl类只能存两个数组的数组,附代码,求解答

难道perl类只能存两个数组的数组,附代码,求解答

Bean.pm
package Bean;

use strict;
use Class::Struct;

struct Bean =>
{
  name => '$',
  list => '@',
};

1;


test.pl

use strict;
use Bean;

my $bean;
my @l;
my @list;
my @beans;

@l = ('111', '112', '113');
push(@list, [@l]);
@l = ('121', '122', '123');
push(@list, [@l]);
@l = ('131', '132', '133');
push(@list, [@l]);
@l = ('141', '142', '143');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name1");
$bean->list(@list);
push(@beans, $bean);

@l = ('211', '212', '213');
push(@list, [@l]);
@l = ('221', '222', '223');
push(@list, [@l]);
@l = ('231', '232', '233');
push(@list, [@l]);
@l = ('241', '242', '243');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name2");
$bean->list(@list);
push(@beans, $bean);

@l = ('311', '312', '313');
push(@list, [@l]);
@l = ('321', '322', '323');
push(@list, [@l]);
@l = ('331', '332', '333');
push(@list, [@l]);
@l = ('341', '342', '343');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name3");
$bean->list(@list);
push(@beans, $bean);

@l = ('411', '412', '413');
push(@list, [@l]);
@l = ('421', '422', '423');
push(@list, [@l]);
@l = ('431', '432', '433');
push(@list, [@l]);
@l = ('441', '442', '443');
push(@list, [@l]);
$bean = Bean->new();
$bean->name("name4");
$bean->list(@list);
push(@beans, $bean);


foreach(@beans)
{
  my $name = $_->name();
  my $list = $_->list();

  print 'name is:' . $name . "\n";
  foreach(@$list)
  {
    my $list = $_;
    foreach(@$list)
    {
      my $str = $_;
      print $str . "\n";
    }
  }
}


报错信息是
Too many args to list at E:\online\oop\test3\test.pl line 20
如果控制scalar(@list)为两个就没问题,为什么呢?
我原来是写java的,对于perl这样的数据结构现在很迷惑.....
太复杂了,搞个简单的,能说明问题的上来。

use strict;
use Bean;

my $bean;
my @l;
my @list;

@l = ('111', '112', '113');
push(@list, [@l]);
@l = ('121', '122', '123');
push(@list, [@l]);
@l = ('131', '132', '133');
push(@list, [@l]);
@l = ('141', '142', '143');
push(@list, [@l]);

$bean = Bean->new();
$bean->name("name1");
$bean->list(@list);


my $listtmp = $bean->list();
foreach(@$listtmp)
{
  my $listtmp2 = $_;
  foreach(@$listtmp2)
  {
    my $str = $_;
    print $str . "\n";
  }
}

您给分析分析
Class::Struct里的文档说的很清楚:

Array ('@' or '*@')

    The element is an array, initialized by default to ().

    With no argument, the accessor returns a reference to the element's whole array (whether or not the element was specified as '@' or '*@').

    With one or two arguments, the first argument is an index specifying one element of the array; the second argument, if present, is assigned to the array element. If the element type is '@', the accessor returns the array element value. If the element type is '*@', a reference to the array element is returned.

    As a special case, when the accessor is called with an array reference as the sole argument, this causes an assignment of the whole array element. The object reference is returned.
$bean->list(@list);
是错误的,struct里面是hash,其value只能为标量,所以上面代码应该是:
$bean->list(\@list);
Hi,
just like Nosferatu said, pls read more perldoc in details.
this is demo code  4u:

[Copy to clipboard] [ - ]
CODE:
use Class::Struct;

struct Breed => {
    name  => '$',
    cross => '$',
};

struct Cat => [
    name     => '$',
    kittens  => '@',
    markings => '%',
    breed    => 'Breed',
   
   
];

struct Bean => {
    list => '@',
};

# main

my $cat = Cat->new(
    name     => 'Socks',
    kittens  => ['Monica', 'Kenneth'],
    markings => { socks=>1, blaze=>"white" },
    breed    => Breed->new(name=>'short-hair', cross=>1),
);   

# Add more name in the list of kittens
push @{$cat->kittens},  ("miao", "mimi", "mini");

print "Once a cat called ", $cat->name, "\n";
print "(which was a ", $cat->breed->name, ")\n";
print "had " . scalar(@{$cat->kittens}) . " kittens: ", join(', ', @{$cat->kittens}), "\n";

#### your Bean for example:
my $obj = Bean->new();
my @arr1 = (1,2,3);
my @arr2 = (4,5,6);
push @{$obj->list}, @arr1; # don't use reference of array !!!
push @{$obj->list}, @arr2;
print "\$obj->list: ", join ", ", @{$obj->list}, "\n";

Mery Christmas and Happy New year!

-- ulmer
谢谢各位,找到错误了

$bean->list(@list);
应该是
$bean->list([@list]);
就好了,^.^