gpt4 book ai didi

perl - 如何在 Perl 中使用数组作为对象属性?

转载 作者:行者123 更新时间:2023-12-04 10:06:00 27 4
gpt4 key购买 nike

我需要一些有关 Perl 数组的帮助

这是我拥有的构造函数。

BuildPacket.pm

     sub new {
my $class = shift;
my $Packet = {
_PacketName => shift,
_Platform => shift,
_Version => shift,
_IncludePath => [@_],
};

bless $Packet, $class;
return $Packet;
}

sub SetPacketName {
my ( $Packet, $PacketName ) = @_;
$Packet->{_PacketName} = $PacketName if defined($PacketName);
return $Packet->{_PacketName};
}

sub SetIncludePath {
my ( $Packet, @IncludePath ) = @_;
$Packet->{_IncludePath} = \@IncludePath;
}

sub GetPacketName {
my( $Packet ) = @_;
return $Packet->{_PacketName};
}

sub GetIncludePath {
my( $Packet ) = @_;
@{ $Packet->{_IncludePath} };
}

(代码已根据'gbacon'的建议进行了修改,谢谢)

我正在以动态方式将相对路径插入 'includeobjects' 数组。正在从 xml 文件中读取包含路径并将其推送到此数组中。
# PacketInput.pm
if($element eq 'Include')
{
while( my( $key, $value ) = each( %attrs ))
{
if($key eq 'Path')
push(@includeobjects, $value);
}
}

所以,包含对象将是这样的:
@includeobjects = (
"./input/myMockPacketName",
"./input/myPacket/my3/*.txt",
"./input/myPacket/in.html",
);

我正在使用这一行来设置包含路径
 $newPacket->SetIncludePath(@includeobjects);

也在 PacketInput.pm , 我有
sub CreateStringPath
{
my $packet = shift;
print "printing packet in CreateStringPath".$packet."\n";
my $append = "";
my @arr = @{$packet->GetIncludePath()};
foreach my $inc (@arr)
{
$append = $append + $inc;
print "print append :".$append."\n";
}
}

我有很多数据包,所以我循环遍历每个数据包
# PacketCreation.pl
my @packets = PacketInput::GetPackets();
foreach my $packet (PacketInput::GetPackets())
{
print "printing packet in loop packet".$packet."\n";
PacketInput::CreateStringPath($packet);
$packet->CreateTar($platform, $input);
$packet->GetValidateOutputFile($platform);
}

get 和 set 方法适用于 PacketName。但是由于 IncludePath 是一个数组,我无法让它工作,我的意思是没有打印相对路径。

最佳答案

如果启用严格编译指示,代码甚至不会编译:

Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 15.Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 29.Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 30.Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 40.

Don't use @ unquoted in your keys because it will confuse the parser. I recommend removing them entirely to avoid confusing human readers of your code.

You seem to want to pull all the attribute values from the arguments to the constructor, so continue peeling off the scalar values with shift, and then everything left must be the include path.

I assume that the components of the include path will be simple scalars and not references; if the latter is the case, then you'll want to make deep copies for safety.

sub new {
my $class = shift;

my $Packet = {
_PacketName => shift,
_Platform => shift,
_Version => shift,
_IncludePath => [ @_ ],
};

bless $Packet, $class;
}

请注意,由于 semantics of Perl subs,没有必要将受祝福的对象存储在临时变量中然后立即返回它。 :

If no return is found and if the last statement is an expression, its value is returned.



下面的方法也将利用此功能。

鉴于上面的构造函数, GetIncludePath变成
sub GetIncludePath {
my( $Packet ) = @_;
my @path = @{ $Packet->{_IncludePath} };
wantarray ? @path : \@path;
}

这里有几件事情正在发生。首先,请注意,我们小心地返回包含路径的副本,而不是对内部数组的直接引用。这样,用户就可以修改 GetIncludePath 返回的值。不必担心破坏数据包的状态。

wantarray operator允许 sub 确定其调用的上下文并做出相应的响应。在列表上下文中, GetIncludePath将返回数组中的值列表。否则,它返回对数组副本的引用。这样,客户端代码可以像这样调用它
foreach my $path (@{ $packet->GetIncludePath }) { ... }

或者
foreach my $path ($packet->GetIncludePath) { ... }
SetIncludePath然后是
sub SetIncludePath {
my ( $Packet, @IncludePath ) = @_;
$Packet->{_IncludePath} = \@IncludePath;
}

请注意,您可以在构造函数中使用类似的代码,而不是使用 shift 一次删除一个参数。 .

您可以使用上面定义的类
#! /usr/bin/perl

use strict;
use warnings;

use Packet;

sub print_packet {
my($p) = @_;
print $p->GetPacketName, "\n",
map(" - [$_]\n", $p->GetIncludePath),
"\n";
}

my $p = Packet->new("MyName", "platform", "v1.0", qw/ foo bar baz /);
print_packet $p;

my @includeobjects = (
"./input/myMockPacketName",
"./input/myPacket/my3/*.txt",
"./input/myPacket/in.html",
);
$p->SetIncludePath(@includeobjects);
print_packet $p;

print "In scalar context:\n";
foreach my $path (@{ $p->GetIncludePath }) {
print $path, "\n";
}

输出:

我的名字
- [foo]
- [酒吧]
- [巴兹]

我的名字
- [./input/myMockPacketName]
- [./input/myPacket/my3/*.txt]
- [./input/myPacket/in.html]

在标量上下文中:
./input/myMockPacketName
./input/myPacket/my3/*.txt
./input/myPacket/in.html

关于perl - 如何在 Perl 中使用数组作为对象属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2561451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com