从哈希函数中返回多个值 [英] Return multiple values from a function in hash
本文介绍了从哈希函数中返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个实质上登录路由器,执行命令,解析命令输出并打印Peer status
和Peer ip
的函数
函数返回预期的值.除了status和peer ip,如果我想返回3个值,例如:$ session_id,$ session_state和$ time.我该如何使用散列呢?我希望函数在哈希中返回所有五个值.
I have created a function which essentially logs into a router,executes a command, parses the command output and prints the Peer status
and Peer ip
Function returns the values as expected. In addition to status and peer ip if i want to return 3 more values say for eg: $session_id, $session_state and $time. How do I do that with hash ? I want the function to return all the five values in a Hash.
sub session_status {
my ($self,$interface_name) = @_;
my $status = 0;
my $peer_ip = 0;
#command to chek the status
my $cmd = 'show crypto session interface ' . $interface_name;
$self->_login();
my $tunnel_status = $self->exec($cmd);
#Regex to match the 'tunnel status' and 'peer ip' string in the cmd output
foreach my $line ( $tunnel_status ) {
if ( $line =~ m/Session\s+status:\s+(.*)/ ) {
$status = $1;
}
if ( $line =~ m/Peer:\s+(\d+.\d+.\d+.\d+)/ ) {
$peer_ip = $1;
}
}
return ($status,$peer_ip);
}
函数调用:
my ($tunnel_status, $peer_ip) = $sshobject->session_status("Tunnel1");
print "TUNNEL STATUS : $tunnel_status\n";
print "PEER IP : $peer_ip\n";
输出:
TUNNEL STATUS : DOWN
PEER IP : 100.81.1.42
推荐答案
尝试一下:
return { 'status' => $status, 'peer_ip' => $peer_ip,
'session_id' => $session_id, 'session_state' => $session_state,
'time' => $time };
然后您可以执行以下操作:
And then you can do this:
my $stuff = $sshobject->session_status("Tunnel1");
print "TUNNEL STATUS : $stuff->{'status'}\n";
print "PEER IP : $stuff->{'peer_ip'}\n";
# etc
这篇关于从哈希函数中返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文