使用 Perl 模块时的最佳实践
best practices when using Perl modules
我基本上是模块的新手,我正在尝试在我的脚本中使用它们。
我无法找到正确使用它们的正确方法,我希望得到你的建议。
让我快速解释一下我要做什么:
我的脚本正在根据来自 XML 个文件的数据进行一些文件传输。
基本上,我有 XML 个文件,内容如下:
<fftg>
<actions>
<!-- Rename file(s) -->
<rename>
<mandatory>0</mandatory>
<file name="foo" to="bar" />
</rename>
<!-- Transfer file(s) -->
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<server>fqdn</server>
<port>22</port>
<file name="bar" remotefolder="toto" />
</transfer>
<!-- Transfer file(s) -->
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<server>fqdn</server>
<port>22</port>
<file name="blabla" remotefolder="xxxx" />
<file name="blabla2" remotefolder="xxxx" />
</transfer>
</actions>
</fftg>
简而言之,我有一个执行 "actions" 的脚本。
每个动作可以重复X次。
现在,我认为最好为我的应用程序创建模块,并将操作放在模块中。
例如:
FFTG::Rename
FFTG::Transfer
FFTG::Transfer::SFTP
FFTG::Transfer::FTP
& 等等(我已经创建了所有这些模块并且它们可以独立工作)
并根据 XML 文件中指定的操作调用这些模块。
如果需要,人们可以创建新的 modules/actions(我希望它是模块化的)。
现在,我不知道如何正确地做到这一点。
所以我的问题是:最好的方法是什么?
目前,我的脚本正在读取这些动作:
# Load XML file
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($FFTG_TSF . "/" . $tid . ".xml");
# Browse XML file
foreach my $transfer ($doc->findnodes('/fftg')) {
# Grab generic information
my($env) = $transfer->findnodes('./environment');
my($desc) = $transfer->findnodes('./description');
my($user) = $transfer->findnodes('./user');
print $env->to_literal, "\n";
# Browse Actions
foreach my $action ($doc->findnodes('/fftg/actions/*')) {
my $actiontype = ucfirst($action->nodeName());
# how do i select a module from the $actiontype here ? ($actiontype = Rename or Transfer)
# i can't do : use FFTG::$actiontype::execaction(); or something for example, it doesnt work
# and is it the right way of doing it ?
}
}
但也许这不是正确的思考方式。 (我正在使用 Lib::LibXML)
我如何调用模块 "dynamically"(在名称中使用变量,例如 FFTG::$actiontype
而且,这是否意味着我必须在每个模块中都有相同的子程序?
示例:子执行
因为我想向模块发送不同的数据......
有什么提示吗?
再次感谢
问候,
首先,你需要想出一个清晰的界面。每个模块都需要具有相同的结构。是否面向对象无关紧要,但它们都需要暴露相同的接口。
这是 FFTG::Rename
的非面向对象实现示例。我遗漏了很多东西,但我想很清楚发生了什么。
package FFTG::Rename;
use strict;
use warnings;
sub run {
my ($args) = @_;
if ($args->{mandatory}) {
# do stuff here
}
# checks args...
# do sanity checks...
return unless -f $args->{file}->{name}; # or whatever...
rename $args->{file}->{name}, $args->{file}->{to} or die $!;
return; # maybe return someting meaningful?
}
现在假设我们有一堆。我们如何加载它们?有几种方法可以做到这一点。我省略了将参数放入 run
函数的部分。您需要从 XML 中获取内容并以与所有这些功能相同的方式传递它,但我认为这与问题无关。
全部加载
最明显的是手动将它们全部加载到脚本中。
#!/usr/bin/env perl
use strict;
use warnings;
use XML::LibXML;
# load FFTG modules
use FFTG::Rename;
# ...
加载完成后,您就可以调用函数了。 exist
keyword 很方便,因为它也可以用来检查函数是否存在。
foreach my $action ( $doc->findnodes('/fftg/actions/*') ) {
my $actiontype = ucfirst( $action->nodeName );
no strict 'refs';
if ( exists &{"FFTG::${actiontype}::run"} ) {
&{"FFTG::${actiontype}::run"}->( $parsed_node_information );
} else {
# this module was not loaded
}
}
不幸的是,非 OO 方法需要 no strict 'refs'
,这并不漂亮。以面向对象的方式进行可能更好。但我会坚持这个答案。
这种方式的明显缺点是您需要始终加载所有模块,并且每当创建新模块时,都需要添加它。这是最不复杂的方式,但也具有最高的维护性。
通过查找自动加载 table
另一种方法是使用自动加载和查找 table 来定义允许的操作。如果您希望您的程序仅按需加载模块,因为您知道您不需要在每次调用时都需要所有模块,但您还希望控制加载的内容,这是有道理的。
可以将加载工作外包给 Module::Runtime。
而不是全部加载
use Module::Runtime 'require_module';
use Try::Tiny;
my %modules = (
'rename' => 'FFTG::Rename',
# ...
);
foreach my $action ( $doc->findnodes('/fftg/actions/*') ) {
try {
no strict 'refs';
require_module $modules{$action};
&{"FFTG::${actiontype}::run"}->($parsed_node_information);
}
catch {
# something went wrong
# maybe the module does not exist or it's not listed in the lookup table
warn $_;
};
}
我还添加了 Try::Tiny 来处理错误。它使您可以控制出现问题时的处理方式。
这种方法可以让您控制允许的操作,如果您偏执,这很好。但它仍然需要您维护脚本并向 %modules
查找 table.
添加新模块
信任并动态加载
第三种最通用的方法是使用 Module::Runtime 动态加载内容而不需要查找 table。
use Module::Runtime 'require_module';
use Try::Tiny;
foreach my $action ( $doc->findnodes('/fftg/actions/*') ) {
try {
my $actiontype = ucfirst($action->nodeName);
require_module "FFTG::${actiontype}";
no strict 'refs';
&{"FFTG::${actiontype}::run"}->($parsed_node_information);
}
catch {
# something went wrong
# the module does not exist
};
}
这种维护最少,但也比较危险。您不知道传入的数据是什么,现在也没有健全性检查。我想不出一种方法来利用我的头脑,但可能有一个。不过,现在不需要编辑脚本并保持模块列表是最新的。
结论
我可能会选择第二种方法。它使您可以控制并仍然保持动态。我不会采用我使用过的非 OOP 方法。
您可以使其保持非 OOP,并且仍然可以通过使用 ->
对象表示法调用 class 方法来摆脱 no strict 'refs'
。然后你的包裹看起来像这样。
package FFTG::Rename;
use strict;
use warnings;
sub run {
my (undef, $args) = @_;
# ...
}
中的undef
是不捕获$class
(不是$self
),因为我们不需要它。或者也许我们这样做,用于日志记录。这取决于。但是有了这个,您基本上可以调用 class 方法来查找 table 解决方案。
require_module $modules{$action};
$modules{$action}->run($parsed_node_information);
这显然更清晰、更可取。
我基本上是模块的新手,我正在尝试在我的脚本中使用它们。 我无法找到正确使用它们的正确方法,我希望得到你的建议。
让我快速解释一下我要做什么:
我的脚本正在根据来自 XML 个文件的数据进行一些文件传输。
基本上,我有 XML 个文件,内容如下:
<fftg>
<actions>
<!-- Rename file(s) -->
<rename>
<mandatory>0</mandatory>
<file name="foo" to="bar" />
</rename>
<!-- Transfer file(s) -->
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<server>fqdn</server>
<port>22</port>
<file name="bar" remotefolder="toto" />
</transfer>
<!-- Transfer file(s) -->
<transfer>
<mandatory>0</mandatory>
<protocol>SFTP</protocol>
<server>fqdn</server>
<port>22</port>
<file name="blabla" remotefolder="xxxx" />
<file name="blabla2" remotefolder="xxxx" />
</transfer>
</actions>
</fftg>
简而言之,我有一个执行 "actions" 的脚本。 每个动作可以重复X次。
现在,我认为最好为我的应用程序创建模块,并将操作放在模块中。
例如:
FFTG::Rename
FFTG::Transfer
FFTG::Transfer::SFTP
FFTG::Transfer::FTP
& 等等(我已经创建了所有这些模块并且它们可以独立工作)
并根据 XML 文件中指定的操作调用这些模块。 如果需要,人们可以创建新的 modules/actions(我希望它是模块化的)。
现在,我不知道如何正确地做到这一点。
所以我的问题是:最好的方法是什么?
目前,我的脚本正在读取这些动作:
# Load XML file
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($FFTG_TSF . "/" . $tid . ".xml");
# Browse XML file
foreach my $transfer ($doc->findnodes('/fftg')) {
# Grab generic information
my($env) = $transfer->findnodes('./environment');
my($desc) = $transfer->findnodes('./description');
my($user) = $transfer->findnodes('./user');
print $env->to_literal, "\n";
# Browse Actions
foreach my $action ($doc->findnodes('/fftg/actions/*')) {
my $actiontype = ucfirst($action->nodeName());
# how do i select a module from the $actiontype here ? ($actiontype = Rename or Transfer)
# i can't do : use FFTG::$actiontype::execaction(); or something for example, it doesnt work
# and is it the right way of doing it ?
}
}
但也许这不是正确的思考方式。 (我正在使用 Lib::LibXML) 我如何调用模块 "dynamically"(在名称中使用变量,例如 FFTG::$actiontype 而且,这是否意味着我必须在每个模块中都有相同的子程序? 示例:子执行
因为我想向模块发送不同的数据......
有什么提示吗? 再次感谢 问候,
首先,你需要想出一个清晰的界面。每个模块都需要具有相同的结构。是否面向对象无关紧要,但它们都需要暴露相同的接口。
这是 FFTG::Rename
的非面向对象实现示例。我遗漏了很多东西,但我想很清楚发生了什么。
package FFTG::Rename;
use strict;
use warnings;
sub run {
my ($args) = @_;
if ($args->{mandatory}) {
# do stuff here
}
# checks args...
# do sanity checks...
return unless -f $args->{file}->{name}; # or whatever...
rename $args->{file}->{name}, $args->{file}->{to} or die $!;
return; # maybe return someting meaningful?
}
现在假设我们有一堆。我们如何加载它们?有几种方法可以做到这一点。我省略了将参数放入 run
函数的部分。您需要从 XML 中获取内容并以与所有这些功能相同的方式传递它,但我认为这与问题无关。
全部加载
最明显的是手动将它们全部加载到脚本中。
#!/usr/bin/env perl
use strict;
use warnings;
use XML::LibXML;
# load FFTG modules
use FFTG::Rename;
# ...
加载完成后,您就可以调用函数了。 exist
keyword 很方便,因为它也可以用来检查函数是否存在。
foreach my $action ( $doc->findnodes('/fftg/actions/*') ) {
my $actiontype = ucfirst( $action->nodeName );
no strict 'refs';
if ( exists &{"FFTG::${actiontype}::run"} ) {
&{"FFTG::${actiontype}::run"}->( $parsed_node_information );
} else {
# this module was not loaded
}
}
不幸的是,非 OO 方法需要 no strict 'refs'
,这并不漂亮。以面向对象的方式进行可能更好。但我会坚持这个答案。
这种方式的明显缺点是您需要始终加载所有模块,并且每当创建新模块时,都需要添加它。这是最不复杂的方式,但也具有最高的维护性。
通过查找自动加载 table
另一种方法是使用自动加载和查找 table 来定义允许的操作。如果您希望您的程序仅按需加载模块,因为您知道您不需要在每次调用时都需要所有模块,但您还希望控制加载的内容,这是有道理的。
可以将加载工作外包给 Module::Runtime。
而不是全部加载use Module::Runtime 'require_module';
use Try::Tiny;
my %modules = (
'rename' => 'FFTG::Rename',
# ...
);
foreach my $action ( $doc->findnodes('/fftg/actions/*') ) {
try {
no strict 'refs';
require_module $modules{$action};
&{"FFTG::${actiontype}::run"}->($parsed_node_information);
}
catch {
# something went wrong
# maybe the module does not exist or it's not listed in the lookup table
warn $_;
};
}
我还添加了 Try::Tiny 来处理错误。它使您可以控制出现问题时的处理方式。
这种方法可以让您控制允许的操作,如果您偏执,这很好。但它仍然需要您维护脚本并向 %modules
查找 table.
信任并动态加载
第三种最通用的方法是使用 Module::Runtime 动态加载内容而不需要查找 table。
use Module::Runtime 'require_module';
use Try::Tiny;
foreach my $action ( $doc->findnodes('/fftg/actions/*') ) {
try {
my $actiontype = ucfirst($action->nodeName);
require_module "FFTG::${actiontype}";
no strict 'refs';
&{"FFTG::${actiontype}::run"}->($parsed_node_information);
}
catch {
# something went wrong
# the module does not exist
};
}
这种维护最少,但也比较危险。您不知道传入的数据是什么,现在也没有健全性检查。我想不出一种方法来利用我的头脑,但可能有一个。不过,现在不需要编辑脚本并保持模块列表是最新的。
结论
我可能会选择第二种方法。它使您可以控制并仍然保持动态。我不会采用我使用过的非 OOP 方法。
您可以使其保持非 OOP,并且仍然可以通过使用 ->
对象表示法调用 class 方法来摆脱 no strict 'refs'
。然后你的包裹看起来像这样。
package FFTG::Rename;
use strict;
use warnings;
sub run {
my (undef, $args) = @_;
# ...
}
中的undef
是不捕获$class
(不是$self
),因为我们不需要它。或者也许我们这样做,用于日志记录。这取决于。但是有了这个,您基本上可以调用 class 方法来查找 table 解决方案。
require_module $modules{$action};
$modules{$action}->run($parsed_node_information);
这显然更清晰、更可取。