我创建了这个函数。
当我打印变量my $bios_current
时,它显示$VAR1 = '200';
,但我的条件if ( $bios_current->responseCode() ne 200)
认为它不是200。
你能帮我吗?这是一个类型问题吗?
sub check_thermalshutdown_settings {
my $host = shift;
if ($host->get_property('summary.hardware.model') eq "ProLiant DL360 Gen9") {
my $error="";
my $bios="";
try {
my $ilo = get_ilo_address($host->name);
my $client = REST::Client->new();
$client->setHost("https://$ilo");
$client->addHeader("Authorization", "Basic blabla==");
eval {
local $SIG{ALRM} = sub { };
alarm 3;
#$client->GET("/redfish/v1/Systems/1/Bios/");
my $bios_current = $client->GET("/redfish/v1/Systems/1/Bios/");
print Dumper $bios_current->responseCode;
alarm 0;
};
if ( $bios_current->responseCode() ne 200) {
$bios = "none";
$error = "Redfish API returned code ".$client->responseCode();
print Dumper $client->responseCode();
} else {
my $json = decode_json($client->responseContent());
#print Dumper $client->responseContent();
#$bios = $json->{'Bios'}->{'Settings'}->{'ThermalShutdown'};
$bios = $json->{'ThermalShutdown'};
#print Dumper $bios;
print Dumper $json->{'ThermalShutdown'};
print "API call is ok\n";
print Dumper $client->setHost("https://$ilo");
}
} catch {
$bios = "none";
$error=$_;
};
发布于 2020-04-27 13:52:12
你的问题与类型无关。
每个Perl程序员应该学习的第一件事是下面两个语句应该出现在每个脚本的顶部。
use strict;
use warnings;
这两个语句捕获了大量错误,其中一个是导致问题的原因。
如果你看看你的eval块
eval {
local $SIG{ALRM} = sub { };
alarm 3;
#$client->GET("/redfish/v1/Systems/1/Bios/");
my $bios_current = $client->GET("/redfish/v1/Systems/1/Bios/");
print Dumper $bios_current->responseCode;
alarm 0;
};
您将看到,变量eval是通过my
修饰符引入的,这将变量的生存期限制在当前作用域,在本例中为$bios_current块。
因此,当您的if语句运行时,变量不再存在,Perl为您创建了一个新的空变量,然后Perl尝试对空变量调用responseCode(),这将失败,通常会终止程序,但是此时您处于try()块中,因此代码不会显示错误,而是跳转到catch块。
https://stackoverflow.com/questions/61460057
复制相似问题