我需要在Perl中编写一个与存储相关的应用程序.该应用程序需要将文件从本地计算机上载到其他一些存储节点.目前,上传方法是FTP,但将来它可能是bittorrent或一些未知的超文件传输方法.
对于需要上传的每个文件,都有一个配置文件,用于定义文件名,文件将上传到的存储节点以及上传过程中应使用的传输方法.
当然,我可以使用以下方法来解决我的问题:
{
if ( $trans_type == "ftp" ) { ###FTP the FILE}
if ( $trans_type == "bit" ) { ###BIT the FILE}
### etc ###
}
Run Code Online (Sandbox Code Playgroud)
但即使我在学校学到了基本的OO知识,我仍然觉得这不是一个好的设计.(问题标题可能有点误导.如果你认为我的问题可以通过非OO解决方案优雅地解决,那对我来说还是可以的.实际上它会更好,因为我的OO知识有限.)
那么你们一般可以给我一些建议吗?当然,如果你提供一些示例代码,这将是一个很大的帮助.
我在C#中进行文本冒险,有人建议我使用调度表而不是switch语句.
这是switch语句代码:
#region Public Methods
public static void Do(string aString)
{
if(aString == "")
return;
string verb = "";
string noun = "";
if (aString.IndexOf(" ") > 0)
{
string[] temp = aString.Split(new char[] {' '}, 2);
verb = temp[0].ToLower();
noun = temp[1].ToLower();
}
else
{
verb = aString.ToLower();
}
switch(Program.GameState)
{
case Program.GameStates.Playing:
if (IsValidInput(Commands, verb, true))
{
switch(verb) //this is the switch statement
{
case "help":
case "?":
WriteCommands();
break;
case "exit": …Run Code Online (Sandbox Code Playgroud) 我试图通过以下方式构建调度:
def run_nn(type=None):
print type, 'nn'
return
def run_svm(type=None):
print type, 'svm'
return
action = {'nn' : run_nn( type=None),
'svm' : run_svm(type=None),}
Run Code Online (Sandbox Code Playgroud)
我希望函数只有在调用时才会执行:
action.get('nn',type='foo')
Run Code Online (Sandbox Code Playgroud)
期望它打印:
foo nn
Run Code Online (Sandbox Code Playgroud)
但它打破了给予:
TypeError: get() takes no keyword arguments
Run Code Online (Sandbox Code Playgroud)
什么是正确的方法呢?
此外,两个功能run_nn(),run_svm()甚至没有被调用执行.我不希望这样.我怎么能避免呢?
首先我要说的是我知道函数指针是如何工作的。如果您想更详细地解释它们,请继续,但是我问您的是如何使用 C 在调度表中实现它们。
我已经搜索过调度表是什么,但除了对它如何工作的模糊理解之外,我并没有真正理解任何东西。
请分享它的实际用途以及我如何用 C 创建我自己的调度表。非常感谢帮助。
假设我有一个带私人调度表的类.
$this->dispatch = array(
1 => $this->someFunction,
2 => $this->anotherFunction
);
Run Code Online (Sandbox Code Playgroud)
如果我再打电话
$this->dispatch[1]();
Run Code Online (Sandbox Code Playgroud)
我得到一个错误,该方法不是一个字符串.当我把它变成这样的字符串时:
$this->dispatch = array(
1 => '$this->someFunction'
);
Run Code Online (Sandbox Code Playgroud)
这会产生 致命错误:调用未定义的函数$ this-> someFunction()
我也试过用:
call_user_func(array(SomeClass,$this->dispatch[1]));
Run Code Online (Sandbox Code Playgroud)
导致消息:call_user_func(SomeClass :: $ this-> someFunction)[function.call-user-func]:第一个参数应该是一个有效的回调.
编辑:我意识到这并没有真正意义,因为当这是SomeClass时它调用SomeClass :: $ this.我已经尝试了几种方法,包含数组
array($this, $disptach[1])
Run Code Online (Sandbox Code Playgroud)
这仍然没有达到我的需要.
结束编辑
如果我没有类并且只有一个包含某些函数的调度文件,则此方法有效.例如,这有效:
$dispatch = array(
1 => someFunction,
2 => anotherFunction
);
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以将这些作为私有方法保留在类中,但仍然将它们与调度表一起使用.
可能重复:
如何在Perl中实现调度表?
我有一个包含诸如int(rand())等命令的哈希表.如何执行这些命令?
这是我目前的想法,但我不知道如何派遣/执行它
my $key;
my @arraydata;
my %commandfunc{
"ab 1", \&func1(\@arraydata),
"ab 2", \&func2(\@arraydata,
"ab 3", \&func3(\@arraydata)
};
foreach $k (keys %commandfunc){
if($something =~ /$k/){ #if $something match with a key string
$key= $k;
#some processing arraydata here;
}
}
#dispatching??
my $command = $commandfunc{$key}->(\@arraydata);
请更正我的代码..非常感谢
我知道这是一个副本不能使用字符串("1")作为子程序ref,而"严格参考"在使用,
但我无法弄清楚我的问题是调用分派表.代码似乎执行,但日志中出现以下错误:Can't use string ("1") as a subroutine ref while "strict refs" in use at C:/filepath/file.pl line 15.
#! C:\strawberry\perl\bin\perl
use strict;
use warnings;
use Custom::MyModule;
use CGI ':standard';
my $dispatch_table = {
getLRFiles => \&Custom::MyModule::getLRFiles,
imageMod => \&Custom::MyModule::imageMod,
# More functions
};
my $perl_function = param("perl_function");
($dispatch_table->{$perl_function}->(\@ARGV) || sub {})->(); # Error occurs on this line
Run Code Online (Sandbox Code Playgroud)
我不确定它是否与我使用自定义模块这一事实有关,而且它可能是愚蠢的,因为我对Perl并不是非常熟悉,但任何帮助都会受到赞赏!