如何使用可扩展代码编写Perl对象?我在思考驱动程序或一些配置,用户可以在其中传递字符串"Classname :: Class"或其他东西.谢谢.
例如,对于图表类:
my $spg = Graph::ShortestPathGraph->new;
$spg->Algorithm( "Graph::DFS" );
$spg->solve;
$spg->Algorithm( "Graph::BFS" );
$spg->solve;
Run Code Online (Sandbox Code Playgroud)
如何编写可扩展代码?
随着计划.假设您正在编写用于绘制一组点的算法.您需要这些点的来源,绘制它们的位置以及用于插入不在集合中的点的算法.
(只是一个注释,假设"图形"在这里意味着"图表",而不是离散数学意义上的图形.)
让我们定义代表这些操作的角色.积分来源必须能够为我们提供以下几点:
package Graphomatic::PointSource;
use Moose::Role;
requires 'get_points'; # return a list of points
1;
Run Code Online (Sandbox Code Playgroud)
绘图仪必须允许我们绘制一个点:
package Graphomatic::Plot;
use Moose::Role;
requires 'plot_point'; # plot a point
requires 'show_graph'; # show the final graph
1;
Run Code Online (Sandbox Code Playgroud)
在给出两个附近点时,插值器必须给出一个点:
package Graphomatic::Interpolate;
use Moose::Role;
requires 'interpolate_point';
1;
Run Code Online (Sandbox Code Playgroud)
现在,我们只需要根据这些角色编写我们的主应用程序:
package Graphomatic;
use Moose;
use Graphomatic::PointSource;
use Graphomatic::Plot;
use Graphomatic::Interpolate;
has 'source' => (
is => 'ro',
does => 'Graphomatic::PointSource',
handles => 'Graphomatic::PointSource',
required => 1,
);
has 'plot' => (
is => 'ro',
does => 'Graphomatic::Plot',
handles => 'Graphomatic::Plot',
required => 1,
);
has 'interpolate' => (
is => 'ro',
does => 'Graphomatic::Interpolate',
handles => 'Graphomatic::Interpolate',
required => 1,
);
sub run { # actually render and display the graph
my $self = shift;
my @points = $self->get_points; # delegated from the PointSource
for my $x (some minimum .. some maximum) {
my ($a, $b) = nearest_points( $x, @points );
$self->plot_point( $self->interpolate_point($a, $b, $x) );
}
$self->show_graph;
}
1;
Run Code Online (Sandbox Code Playgroud)
现在,定义一些源实现是一件简单的事情.让我们从文件中读取点:
package Graphomatic::PointSource::File;
use Moose;
use MooseX::FileAttribute;
# ensure, at compile-time, that this class is a valid point
# source
with 'Graphomatic::PointSource';
has_file 'dataset' => ( must_exist => 1, required => 1 );
sub get_points {
my $self = shift;
return parse $self->dataset->slurp;
}
1;
Run Code Online (Sandbox Code Playgroud)
并绘制到Z窗口系统:
package Graphomatic::Plot::Z;
use Moose;
use Z;
with 'Graphomatic::Plot';
has 'window' => ( is => 'ro', isa => 'Z::Window', lazy_build => 1);
sub _build_window { return Z->new_window }
sub plot_point {
my ($self, $point) = @_;
$self->window->plot_me_a_point_kthx($point->x, $point->y);
}
sub show_plot {
my $self = shift;
$self->window->show;
}
1;
Run Code Online (Sandbox Code Playgroud)
并使用随机数生成器进行插值(嘿,我很懒,而且我不会查找双三次插值:P):
package Graphomatic::Interpolate::Random;
use Moose;
with 'Graphomatic::Interpolate';
sub interpolate_point {
my ($self, $a, $b, $x) = @_;
return 4; # chosen by fair dice roll.
# guaranteed to be random.
}
1;
Run Code Online (Sandbox Code Playgroud)
现在我们可以将所有部分组合成一个工作程序:
use Graphomatic::PointSource::File;
use Graphomatic::Plot::Z;
use Graphomatic::Interpolate::Random;
my $graphomatic = Graphomatic->new(
source => Graphomatic::PointSource::File->new(
file => 'data.dat',
),
plot => Graphomatic::Plot::Z->new,
interpolate => Graphomatic::Interpolate::Random->new,
);
$graphomatic->run;
Run Code Online (Sandbox Code Playgroud)
现在,只需实现"执行"所需角色的新类,您就可以干净地自定义任何部件而不会影响其他部件.(如果他们说'with ...'并且他们不符合要求,那么一旦加载该类,就会出现错误.如果您尝试使用实例作为不"执行"的参数正确的角色,构造函数会死.
输入安全性,这是一件很棒的事情.)
至于处理配置文件,只是以某种方式读取名称和参数,然后:
my $interpolate_class = get_config('interpolate_class');
Class::MOP::load_class($interpolate_class);
my $interpolate = $interpolate_class->new( %interpolate_class_args );
my $graphomatic = Graphomatic->new( interpolate => $interpolate, ... );
Run Code Online (Sandbox Code Playgroud)
MooseX :: YAML是一种很好的自动化方式.