是否有一个 Perl 模块可以在运行时动态地将 YAML 文件转换为 Moose 对象?

发布于 2024-09-27 22:11:44 字数 180 浏览 2 评论 0原文

我一直在尝试找到一个 Perl 模块,可以将 YAML 文件转换为 moose 对象,而无需像使用 MooseX::YAML。有谁知道这样的模块(或脚本)?

I've been trying to find a Perl module that converts a YAML file into moose objects without having to pre-declare the structure as you seem to need to do when using MooseX::YAML. Does anyone know of such a module (or script)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池予 2024-10-04 22:11:45

如果您不想在 YAML 中执行任何特殊操作,则只需设置
对您的 Moose 类进行适当的强制转换,然后将加载的数据传递给
new()。例如:

package My::Types;
use Moose::Util::TypeConstraints;

class_type 'My::Related::Class';
coerce 'My::Related::Class', from 'HashRef',
    via { My::Related::Class->new(%$_) };

package My::Class;

use Moose;

has name => (is => 'ro', isa => 'Str');
has related => (is => 'ro', isa => 'My::Related::Class', coerce => 1);

package My::Related::Class;

use Moose;
has flavor => (is => 'ro', isa => 'Str');

然后,在某些 YAML 中:

name: "An instance of My::Class"
related: 
    flavor: "Dangerberry!"

然后,在某些代码中:

my $obj = My::Class->new(Load($yaml_data));

print ref $obj->related; # My::Related::Class
print $obj->related->flavor; # Dangerberry!

显然,如果没有对 Moose 类进行更多自定义,这不是可往返的——它只是用于构造对象。此外,您还需要知道根对象是什么类。

不过,对于许多简单的用途来说,这可能已经足够了。

If you don't want to do anything special in your YAML, you can just set up the
proper coercions for your Moose classes and then pass the loaded data to
new(). For example:

package My::Types;
use Moose::Util::TypeConstraints;

class_type 'My::Related::Class';
coerce 'My::Related::Class', from 'HashRef',
    via { My::Related::Class->new(%$_) };

package My::Class;

use Moose;

has name => (is => 'ro', isa => 'Str');
has related => (is => 'ro', isa => 'My::Related::Class', coerce => 1);

package My::Related::Class;

use Moose;
has flavor => (is => 'ro', isa => 'Str');

Then, in some YAML:

name: "An instance of My::Class"
related: 
    flavor: "Dangerberry!"

Then, in some code:

my $obj = My::Class->new(Load($yaml_data));

print ref $obj->related; # My::Related::Class
print $obj->related->flavor; # Dangerberry!

This isn't roundtrippable, obviously, without some more customization of your Moose classes -- it's just for constructing objects. Also, you need to know what classes the root objects are.

That's probably enough for a lot of simple uses, though.

如歌彻婉言 2024-10-04 22:11:44

不。

Moose 类、它们的属性以及属于它们的其他内容都附加有大量元数据。您无法从单个实例的数据推断出所有元数据。

我假设,给定一个

---
foo: 42
bar: ['moo', 'kooh']

您期望的 yaml 文档,并返回响应对 foo 和 bar 方法的调用的对象,返回各自的值。但这些访问器应该如何表现呢?它们应该是简单的读取方法,还是也允许写入?他们应该针对任何类型的约束进行验证吗?等等。

如果您真正需要的只是使一些不受欢迎的数据结构像对象一样可访问,请查看 Data::Hive< code>Hash::AsObject 以及类似的模块。

如果您确实想构建正确的 Moose 类,并且可以接受所涉及的猜测,或者碰巧在某处有必要的元数据,那么您可以使用元协议。

my $class = Moose::Meta::Class->create_anon_class(
    attributes => [map {
        # your particular set of assumptions here
        Moose::Meta::Attribute->new($_ => (is => 'ro', ...))
    } keys %{ $deserialized_yaml }],
);

my $instance = $class->name->new($deserialized_yaml);
$instance->$some_key_in_the_yaml_document;

Don't.

Moose classes, their attributes, and whatever else belongs to them, have a lot of meta-data attached to them. You can't infer all of that meta-data from the data of a single instance.

I'm assuming, given a yaml document as

---
foo: 42
bar: ['moo', 'kooh']

you'd expect and object back that responds to calls to a foo and a bar method, returning the respective values. But how should those accessors behave? Should they be simple reader-methods, or also allow writing? Should they validate against any kind of typeconstraint? etc.

If all you really need is something that makes some unblessed data-structure accessible like an object, have a look at Data::Hive, Hash::AsObject, and similar modules instead.

If you really want to build proper Moose classes, and are either alright with the guesswork that'd be involved, or happen to have the necessary meta-data available somewhere, you can just use the meta-protocol.

my $class = Moose::Meta::Class->create_anon_class(
    attributes => [map {
        # your particular set of assumptions here
        Moose::Meta::Attribute->new($_ => (is => 'ro', ...))
    } keys %{ $deserialized_yaml }],
);

my $instance = $class->name->new($deserialized_yaml);
$instance->$some_key_in_the_yaml_document;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文