如何在Perl中将数组用作对象属性?

时间:2010-04-01 15:18:17

标签: perl arrays constructor

我需要一些关于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文件中读取include路径并将其推送到此数组中。

# 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是一个数组,我无法使它工作,我的意思是相对路径没有被打印。

3 个答案:

答案 0 :(得分:9)

如果启用strict pragma,则代码甚至无法编译:

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.

不要在密钥中使用@,因为它会使解析器混淆。我建议完全删除它们,以免让人类读者感到困惑。

您似乎想要从参数中将所有属性值拉到构造函数中,因此继续使用shift剥离标量值,然后剩下的一切必须是包含路径。

我假设include路径的组件是简单的标量而不是引用;如果后者是这种情况,那么你将需要为安全做出深刻的副本。

sub new {
  my $class = shift;

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

  bless $Packet, $class;
}

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

  

如果未找到return且最后一个语句是表达式,则返回其值。

以下方法也将使用此功能。

鉴于上面的构造函数,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";
}

输出:

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

答案 1 :(得分:4)

减少打字的另一种方法是使用Moose。

package Packet;
use Moose::Policy 'Moose::Policy::JavaAccessors';
use Moose;

has 'PacketName' => (
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);

has 'Platform' => (
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);

has 'Version' => (
    is       => 'rw',
    isa      => 'Int',
    required => 1,
);

has 'IncludePath' => (
    is       => 'ro',
    isa      => 'ArrayRef[Str]',
    default  => sub {[]},
    traits => [ 'Array' ],
    handles => {
        getIncludePath       => 'elements',
        getIncludePathMember => 'get',
        setIncludePathMember => 'set',
    },
);

__PACKAGE__->meta->make_immutable;
no Moose;
1;

查看Moose::Manual::Unsweetened,了解Moose如何节省时间的另一个例子。

如果您坚持学习古典Perl OOP的愿望,请阅读以下perldoc文章:perlbootperltootperlfreftutperldsc

一本关于经典Perl OO的好书是Damian Conway's Object Oriented Perl。它将让您了解Perl对象的可能性。

答案 2 :(得分:3)

了解@gbacon's answer后,您可以使用Class::Accessor::Fast保存一些输入:

#!/usr/bin/perl

package My::Class;
use strict; use warnings;
use base 'Class::Accessor::Fast';

__PACKAGE__->follow_best_practice;
__PACKAGE__->mk_accessors( qw(
    IncludePath
    PacketName
    Platform
    Version
));

use overload '""' => 'to_string';

sub to_string {
    my $self = shift;
    sprintf(
        "%s [ %s:%s ]: %s",
        $self->get_PacketName,
        $self->get_Platform,
        $self->get_Version,
        join(':', @{ $self->get_IncludePath })
    );
}

my $obj = My::Class->new({
        PacketName => 'dummy', Platform => 'Linux'
});
$obj->set_IncludePath([ qw( /home/include /opt/include )]);
$obj->set_Version( '1.05b' );
print "$obj\n";