如何在perl中使用哈希映射

如何在perl中使用哈希映射,perl,perl-module,Perl,Perl Module,我创建了一个函数来运行iperf流量。代码按预期工作。该函数可以同时支持TCP和UDP协议。如果协议未作为参数传递,则默认情况下使用TCP。对于UDP,-u作为参数传递。我使用了,除非此支票使用了语句。我被要求使用hashmap而不是除非语句。有人能告诉我如何在这个场景中使用hashmap吗 sub start_iperf_on_client_forward_traffic { my ($self,$cli_interface_ip,$origin_interface_ip, $por

我创建了一个函数来运行iperf流量。代码按预期工作。该函数可以同时支持TCP和UDP协议。如果协议未作为参数传递,则默认情况下使用TCP。对于UDP,-u作为参数传递。我使用了
,除非此支票使用了
语句。我被要求使用hashmap而不是除非语句。有人能告诉我如何在这个场景中使用hashmap吗

sub start_iperf_on_client_forward_traffic {

    my ($self,$cli_interface_ip,$origin_interface_ip, $port,$protocol) = @_;

    unless (defined $protocol)
    {
      $protocol = "";
    }

    # start iperf3 on client. If protocol is not specified tcp protocol will be used..
    $self->{ssh}->execute( 'iperf3 ' . $protocol . ' -c ' . $origin_interface_ip . ' -B ' .
      $cli_interface_ip . ' -i ' . 2 .  ' -p ' . $port . ' -l ' . 576.
        ' -b ' . 100 . 'M ' . ' -k ' . 1000 );

    my @stdout = $self->{ssh}->stdOut();

    print  Dumper  @stdout;
}
函数调用:

  $self->{'traffic_obj'} =  Iperf->new(ip => "198.18.193.151" )
    $self->{'client'}   =  $self->{'traffic_obj_1'}->start_iperf_on_client_forward_traffic('9.1.1.2','8.2.0.2','7755','-u');
输出:

 $VAR1 = 'Connecting to host 8.2.0.2, port 7755';
    $VAR2 = '[  4] local 9.1.1.2 port 47105 connected to 8.2.0.2 port 7755';
    $VAR3 = '[ ID] Interval           Transfer     Bandwidth       Total Datagrams';
    $VAR4 = '[  4]   0.00-2.31   sec  5.49 MBytes  20.0 Mbits/sec  10000  ';
    $VAR5 = '- - - - - - - - - - - - - - - - - - - - - - - - -';
    $VAR6 = '[ ID] Interval           Transfer     Bandwidth       Jitter    Lost/Total Datagrams';
    $VAR7 = '[  4]   0.00-2.31   sec  5.49 MBytes  20.0 Mbits/sec  0.279 ms  3751/9987 (38%)  ';
    $VAR8 = '[  4] Sent 9987 datagrams';
    $VAR9 = '';
    $VAR10 = 'iperf Done.';

你问题的核心在于以下陈述和问题:

我被要求使用hashmap而不是除非语句。 有人能告诉我如何在这个场景中使用哈希映射吗

sub start_iperf_on_client_forward_traffic {

    my ($self,$cli_interface_ip,$origin_interface_ip, $port,$protocol) = @_;

    unless (defined $protocol)
    {
      $protocol = "";
    }

    # start iperf3 on client. If protocol is not specified tcp protocol will be used..
    $self->{ssh}->execute( 'iperf3 ' . $protocol . ' -c ' . $origin_interface_ip . ' -B ' .
      $cli_interface_ip . ' -i ' . 2 .  ' -p ' . $port . ' -l ' . 576.
        ' -b ' . 100 . 'M ' . ' -k ' . 1000 );

    my @stdout = $self->{ssh}->stdOut();

    print  Dumper  @stdout;
}
你真的解释得不够充分,无法自信地回答。除非
语句将参数设置为空字符串(如果未传递)(即未定义)。我已经看过了
iperf3
,如果未提供
-u
开关,则默认行为是使用TCP,因此,您所做的是有意义的。不幸的是,没有办法(至少我可以看到)以某种方式“…使用hashmap代替”

我所能做的最好的建议是,请求中可能存在误解,要求您使用hashref将参数传递给子例程

随着向子对象(或方法)添加更多参数,将适当的参数与适当的位置匹配变得越来越困难,因此代码也变得越来越脆弱。解决此问题的常用方法是使用命名参数而不是位置参数,因为顺序不相关。在perl5中,这通常意味着传递命名参数的散列(或使用“胖逗号”的数组,给出与散列相同的外观)

因此,您的呼叫将如下所示:

$self->{'client'}  =  $self->{'traffic_obj_1'}->
                      start_iperf_on_client_forward_traffic({
                          cli_ip    =>  '9.1.1.2' ,
                          origin_ip =>  '8.2.0.2' ,
                          port      =>  '7755'    ,
                          protocol  =>   '-u'
                       });
…而你日常生活的开场白看起来像

sub start_iperf_on_client_forward_traffic {
    my ($self, $args) = @_;
…但您仍然需要进行类似的检查,以查看可选参数(本例中为“协议”)是否通过

my $protocol = $args->{protocol} // "" ;   # Set to "" if not passed

但所有这些都是基于对你最初的真实意思的猜测。

散列中有什么?或者,为什么要在这里使用哈希?