Joe*_*ger 7 perl moose storable pdl
我是Moose的新手并且做得很好,直到我用PDL作为财产.我希望能够将一个对象写入一个文件(我一直在使用use MooseX::Storage; with Storage('io' => 'StorableFile');
,这个对象有PDL
一个属性.PDL::IO::Storable
提供了Storable
以这种方式使用的必要方法,但是我不知道如何在驼鹿.
这是一个例子,它有点长,我知道,但它尽可能少,我可以做到:
#!/usr/bin/perl
package LinearPDL;
use Moose;
use PDL::Lite;
use PDL::IO::Storable;
use MooseX::Storage;
with Storage('io' => 'StorableFile');
has 'length' => (is => 'ro', isa => 'Num', required => 1);
has 'divisions' => (is => 'ro', isa => 'Int', required => 1);
has 'linear_pdl' => (is => 'ro', isa => 'PDL', lazy => 1, builder => '_build_pdl');
sub _build_pdl {
my $self = shift;
my $pdl = $self->length() / ( $self->divisions() - 1 ) * PDL::Basic::xvals($self->divisions());
return $pdl;
}
no Moose;
__PACKAGE__->meta->make_immutable;
use strict;
use warnings;
my $linear_pdl = LinearPDL->new('length' => 5, 'divisions' => 10);
print $linear_pdl->linear_pdl;
$linear_pdl->store('file'); # blows up here!
my $loaded_lpdl = load('file');
print $loaded_lpdl->linear_pdl;
Run Code Online (Sandbox Code Playgroud)
我想我可能不得不制作一个PDL类型,或者甚至可能将PDL包装成某些东西(使用MooseX::NonMoose::InsideOut
),但也许有人可以将我从中拯救出来(或者指出我是正确的道路,如果是的话).
你没有说出实际出了什么问题.猜测你需要告诉MooseX :: Storage如何使用PDL对象的Storable钩子来处理PDL对象.MooseX :: Storage中此功能的文档非常差,但MooseX :: Storage :: Engine有一个add_custom_type_handler()
方法,它接受一个typename(在你的情况下是PDL)和一个HashRef的处理程序.
MooseX::Storage::Engine->add_custom_type_handler(
'PDL' => (
expand => sub { my ($data) = @_; ... },
collapse => sub { my ($object) = @_; ... },
)
);
Run Code Online (Sandbox Code Playgroud)
请转过#moose
irc.perl.org或Moose邮件列表并询问.
[编辑:使用基于测试的示例进行更新.]