如何在 Perl 中实现调度表? [英] How do I implement dispatch tables in Perl?

查看:59
本文介绍了如何在 Perl 中实现调度表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用 Perl 编写一个与存储相关的应用程序.该应用程序需要将文件从本地机器上传到其他一些存储节点.目前,上传方式是FTP,但未来可能是bittorrent或一些未知的超级文件传输方式.

I need to write a storage related app in Perl. The app needs to upload files from the local machine to some other storage nodes. Currently, the uploading method is FTP, but in the future it may be bittorrent or some unknown super-file-transferring method.

对于每个需要上传的文件,都有一个配置文件,它定义了文件名、文件将上传到的存储节点以及上传过程中应该使用的传输方式.

For every file that needs to be uploaded, there is a configuration file which defines the file name, the storage node which the file will be uploaded to and what transferring method should be used during the uploading.

当然,我可以用下面的方法来解决我的问题:

Of course, I can use the following method to solve my problem:

{
  if ( $trans_type == "ftp" ) { ###FTP the FILE}
  if ( $trans_type == "bit" ) { ###BIT the FILE}
  ### etc ###
}

但是即使我在学校学到了基本的OO知识,我仍然觉得这不是一个好的设计.(问题标题可能有点误导.如果你认为我的问题可以用非面向对象的解决方案优雅地解决,对我来说还可以.实际上它会更好,因为我的面向对象知识有限.)

But even with my basic OO knowledge learned in school, I still feel that this is not a good design. (The question title might be a little bit misleading. If you think my problem can solved gracefully with a non-OO solution, it's quite OK for me. Actually it will be better, since I have limited OO knowledge.)

你们能给我一些总体建议吗?当然,如果你也提供一些示例代码,这将是一个很大的帮助.

So could you guys give me some advice in general? Of course, if you provide some sample code as well, this will be a great help.

推荐答案

首先,Perl 中的字符串相等性测试是 eq,而不是 ==.

First, string equality testing in Perl is eq, not ==.

如果你有方法来完成这项工作,比如named bit和ftp,

If you have methods to do the work, say named bit and ftp,

my %proc = (
    bit => \&bit,
    ftp => \&ftp,
);

my $proc = $proc{$trans_type};
$proc->() if defined $proc;

这篇关于如何在 Perl 中实现调度表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆