gpt4 book ai didi

perl - perl 类中的多个数据成员

转载 作者:行者123 更新时间:2023-12-01 11:41:19 24 4
gpt4 key购买 nike

我是 perl 的新手,并且仍在学习 perl 中的 oop。我通常使用 CC++ 编写代码。要求bless一个对象通知perl先在那个包里搜索方法。这就是祝福所做的。然后在 -> 的帮助下进行的每个函数调用都将 instance 本身作为第一个参数传递。现在我对为新对象编写构造函数有疑问。通常构造函数通常看起来像:

sub new {
my %hash = {};
return bless {%hash}; #will automatically take this package as the class
}

现在我想在我的类中有两个数据成员,这样我就可以做这样的事情:

sub new {
my %hash = {};
$hash->{"table_header"} = shift @_; #add element to hash
$hash->{"body_content"} = shift @_;
return bless {%hash}; #will automatically take this package as the class
}

我的问题是,这是唯一可能的方法吗?我们不能像在 CC++ 中那样拥有多个数据成员,而且我们必须使用像“table_header”和“body_content”这样的字符串。

编辑:在 C 或 C++ 中,我们可以直接引用数据成员(现在假设它是公共(public)的)。这里有一个必须做的额外引用。我想知道是否有任何方法可以让我们拥有一个类似 C 的对象。

sub new {
my $table_header = shift @_;
my $body_content = shift @_;
#bless somehow
}

希望这能消除一些困惑。

最佳答案

有一些模块可以使 Perl 中的 OOP 更容易。最重要的是Moose :

use strict; use warnings;

package SomeObject;
use Moose; # this is now a Moose class

# declare some members. Note that everything is "public"
has table_header => (
is => 'ro', # read-only access
);
has body_content => (
is => 'rw', # read-write access
);

# a "new" method is autogenerated

# some method that uses these fields.
# Note that the members can only be accessed via methods.
# This guards against typos that can't be easily caught with hashes.
sub display {
my ($self) = @_;
my $underline = "=" x (length $self->table_header);
return $self->table_header . "\n" . $underline . "\n\n" . $self->body_content . "\n";
}

package main;

# the "new" takes keyword arguments
my $instance = SomeObject->new(
table_header => "This is a header",
body_content => "Some body content",
);

$instance->body_content("Different content"); # set a member

print $instance->display;

# This is a header
# ================
#
# Different content

如果您开始了解 Moose,您会发现一个比 Java 或 C++ 中的对象系统灵活得多的对象系统,因为它采用了 Perl6 和 Common Lisp 对象系统的思想。当然,这很丑陋,但在实践中效果很好。

由于 Perl OOP 的工作方式,不可能让实例成员作为变量单独访问。嗯,差不多。实验性 mop 模块就是这样做的。

use strict; use warnings;
use mop;

class SomeObject {
# Instance variables start with $!..., and behave like ordinary variables
# If you make them externally accessible with "is ro" or "is rw", then
# appropriate accessor methods are additionally generated.

# a private member with public read-only accessor,
# which has to be initialized in the constructor.
has $!table_header is ro = die 'Please specify a "table_header"!';

# a private member with public read-write accessor,
# which is optional.
has $!body_content is rw = "";

# new is autogenerated, as in Moose

method display() {
# arguments are handled automatically, so we could also do $self->table_header.
my $underline = "=" x (length $!table_header);
return "$!table_header\n$underline\n\n$!body_content\n";
}
}

# as seen in Moose
my $instance = SomeObject->new(
table_header => "This is a header",
body_content => "Some body content",
);

$instance->body_content("Different content"); # set a member, as in Moose

print $instance->display;

# This is a header
# ================
#
# Different content

虽然它有漂亮的语法,但现在不要将 mop 用于严肃的项目,而是坚持使用 Moose。如果 Moose 对您来说太重量级,那么您可能会喜欢更轻的替代品,例如 Mouse 或 Moo(这三个对象系统大多相互兼容)。

关于perl - perl 类中的多个数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417369/

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