eou*_*uti 5 variables perl constructor instance
我写了perl类,但我不知道如何在我的$this变量中有一个数组或一个哈希?
我有一个pack.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;
Run Code Online (Sandbox Code Playgroud)
我想有一个@tab,我可以通过访问$this->tab,但我不想在arg中给它实例.
它在Perl中如何工作?
谢谢.
鉴于您对我的评论的回答,我认为您想要
my($this) = {
"nom" => $nom,
"nbports" => $nbports,
"gio" => $gio,
"tab" => []
};
Run Code Online (Sandbox Code Playgroud)
即设置 $this->{tab} 为对新匿名数组的引用。
现在您可以根据需要引用它,例如
$this->{"tab"}[0] = "new value";
print "Table contains ", scalar(@{$this->{"tab"}}), "entries\n";
Run Code Online (Sandbox Code Playgroud)