这是我的构造函数.
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} }; }
我以动态的方式将相对路径推入’includeobjects’数组.正在从xml文件中读取包含路径并将其推送到此数组中.
# PacketInput.pm if($element eq 'Include') { while( my( $key,$value ) = each( %attrs )) { if($key eq 'Path') push(@includeobjects,$value); } }
所以,includeobject将是这样的:
@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.
不要在密钥中使用@ unquoted,因为它会使解析器混淆.我建议完全删除它们,以免混淆人类读者的代码.
您似乎想要将所有属性值从参数拉到构造函数,因此继续使用shift
剥离标量值,然后剩下的一切必须是包含路径.
我假设包含路径的组件将是简单的标量而不是引用;如果后者是这种情况,那么你将需要为安全制作深层副本.
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允许子确定其呼叫的上下文并相应地做出响应.在列表上下文中,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",); $p->SetIncludePath(@includeobjects); print_packet $p; print "In scalar context:\n"; foreach my $path (@{ $p->GetIncludePath }) { print $path,"\n"; }
输出:
MyName - [foo] - [bar] - [baz] MyName - [./input/myMockPacketName] - [./input/myPacket/my3/*.txt] - [./input/myPacket/in.html] In scalar context: ./input/myMockPacketName ./input/myPacket/my3/*.txt ./input/myPacket/in.html