Perl,Perl构造函数中的@array

Perl,Perl构造函数中的@array,perl,variables,constructor,instance,Perl,Variables,Constructor,Instance,我编写perl类,但我不知道如何在$this变量中包含数组或哈希 我有一个包.pm: #!/usr/bin/perl -w use strict; use Parallel::ForkManager; package Pack; our $cgi = new CGI; sub new { my ($classe, $nom, $nbports, $gio) = @_; my $this = { "nom" => $nom, "nbports" =

我编写perl类,但我不知道如何在
$this
变量中包含数组或哈希

我有一个
包.pm

#!/usr/bin/perl -w
use strict;
use Parallel::ForkManager;
package Pack;

our $cgi = new CGI;

sub new {
    my ($classe, $nom, $nbports, $gio) = @_;

    my $this = {
    "nom"    => $nom,
    "nbports" => $nbports,
    "gio"   => $gio
    };

    bless($this, $classe);
    return $this;   
}
    ...
1;
我想要一个
@tab
,我可以通过
$this->tab
访问,但我不想在arg中将它提供给实例。
它在Perl中是如何工作的

谢谢。

试试:

sub set_tab {
my ($self, @tab) = @_;
$self->{ tab } = \@tab;
}

鉴于你对我评论的回答,我想你想要

my($this) = {
    "nom"     =>  $nom,
    "nbports" =>  $nbports,
    "gio"     =>  $gio,
    "tab"     =>  []
};
i、 e.将$this->{tab}设置为对新匿名数组的引用

现在你可以随意引用它,例如

$this->{"tab"}[0] = "new value";
print "Table contains ", scalar(@{$this->{"tab"}}), "entries\n";
考虑根据您的OO Perl需求使用

我创建了一个对象的Moose版本,其中包含一个属性,该属性具有数组特征委托,包括委托方法的curry。Moose提供了简单的方法来生成功能强大的封装类,而无需编写大量的样板文件

我创建了一个具有以下属性的类:
nom
nbport
gio
选项卡

nom
是只读字符串,在创建对象时是必需的。
nbport
为只读整数值,未提供时默认为32。
gio
是可选的读写布尔值。
选项卡是一个字符串数组。已为
选项卡定义了各种行为:

  • 所有选项卡
    返回
    选项卡的内容列表
  • add_tab
    将值推送到
    tabs
  • tab\u count
    返回
    tabs
  • alpha_tabs
    按字母顺序返回
    选项卡的成员列表
  • turn_tabs
    返回
    tabs
    中的字符串列表,但字母顺序相反
使用设置属性的任何尝试都必须检查类型正确性

Moose使用以下代码创建所有必需的方法来支持这些复杂行为:

package Pack;
use Moose;

has 'nom' => (
   is => 'ro',
   isa => 'Str',
   required => 1,
);

has 'nbports' => (
   is      => 'ro',
   isa     => 'Int',
   default => 32,
);

has 'gio' => (
   is  => 'rw',
   isa => 'Bool',
   predicate => 'has_gio',
);

has 'tab' => (
   traits  => ['Array'],
   is      => 'ro',
   isa     => 'ArrayRef[Str]',
   default => sub {[]},
   handles => {
      all_tabs    => 'elements',
      add_tab     => 'push',
      turn_tabs   => [ 'map', sub { reverse } ],
      tab_count   => 'count',
      alpha_tabs  => [ 'sort', sub { lc($a) cmp lc($b) } ],
  },
);

__PACKAGE__->meta->make_immutable;
no Moose;
1;
可以这样使用:

 my $p = Pack->new( nom => 'Roger', tab => [qw( fee fie foe fum )] );

 my $gio_state = 'UNSET';
 if( $p->has_gio ) {
      $gio_state = $p->gio ? 'TRUE' : 'FALSE';
 }

 print  "GIO is $gio_state\n";

 my @turned = $p->turn_tabs; # eef eif eof muf
 $p->add_tabs( 'faa', 'fim' );
 my @sorted = $p->alpha_tabls; # faa fee fie fim foe fum

 my $count = $p->tab_count;  # 6

 my $ports = $p->nbports; # 32

为了完整性,我会添加。还有
sub get_tab{my$this=shift;return@{$this->{tab};}
你想做什么?是否希望有一个@tab,它是$this的一部分,但不传递给new()?是否要将其创建为空表,以便以后可以将其作为$this->tab使用?@AAT:是的,我想要$this的@tab部分,并且我不会传递给new(),一个在实例化时创建的空数组。我可以通过$this->tab访问。ThxThanks,这就是我需要的,如何访问I元素
print$this->{“tab”}[i]
?是否可以对哈希执行相同的操作<代码>“tab”=>{}
?@eouti--1)应该这样做,2)是的。$this本身只是对匿名散列的引用,没有理由不能反过来包含对以相同方式创建的其他散列的引用。@eouti:如何创建perl数据结构并引用它们,您可以阅读