我是 Moose 的新手,在使用 PDL 作为属性遇到障碍之前做得很好。我希望能够将对象写入文件(我一直在使用use MooseX::Storage; with Storage('io' => 'StorableFile');
,并且该对象具有 aPDL
作为属性。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;
我想我可能必须创建一个 PDL 类型,甚至可能将 PDL 包装到某些东西中(使用MooseX::NonMoose::InsideOut
),但也许有人可以将我从中拯救出来(或者如果是的话,可以为我指明正确的道路)。