此条件是否检查错误是否为 HASH 且是否具有参数 perl?
Is this condition checking if the error is a HASH and has arguments perl?
好的,这只是一个关于条件语句的简单问题,我只是想确保我的条件为真,而不是因为语法错误或测试不正确而执行。
# Method for creating error message
sub new {
my ( $class, $error, %args ) = @_;
# Initialize error with data
my $self = $error;
# If the error contains context parameters... Insert parameters into string template
if('HASH' && %args) {
foreach my $key (@{ $self->{context} } ) {
# And take the ones we need
$self->{args}->{$key} = $args{$key};
}
my @template_args = map { $self->{args}->{$_} } @{ $self->{context} };
# map/insert arguments into context hash and insert into string template
$self->{message} = sprintf ($self->{template}, @template_args);
}
return bless $self, $class;
}
这就是我正在做的,但它导致我的构建失败:
if($self eq 'HASH' && %args) {
doStuff();
}
实际上现在看到这个,它看起来不对,因为我正在将 self 与 HASH 和 %args 进行比较
您的原始状态中缺少 ref
。
$self eq 'HASH' && %args
将检查 $self
是否是字符串 'HASH'
。这可能不是你想要的。你想检查 $self
的 reference type,所以你必须做:
ref $self eq 'HASH' && %args
现在它将检查引用的类型,如果是 HASH
,则 return 为真。
你的完整程序代码肯定不是你想要的
'HASH' && %args
字符串'HASH'
始终为真。 %args
将 return %args
的元素数量,因为它是标量上下文,因此如果散列中有任何内容,它将为真。
好的,这只是一个关于条件语句的简单问题,我只是想确保我的条件为真,而不是因为语法错误或测试不正确而执行。
# Method for creating error message
sub new {
my ( $class, $error, %args ) = @_;
# Initialize error with data
my $self = $error;
# If the error contains context parameters... Insert parameters into string template
if('HASH' && %args) {
foreach my $key (@{ $self->{context} } ) {
# And take the ones we need
$self->{args}->{$key} = $args{$key};
}
my @template_args = map { $self->{args}->{$_} } @{ $self->{context} };
# map/insert arguments into context hash and insert into string template
$self->{message} = sprintf ($self->{template}, @template_args);
}
return bless $self, $class;
}
这就是我正在做的,但它导致我的构建失败:
if($self eq 'HASH' && %args) {
doStuff();
}
实际上现在看到这个,它看起来不对,因为我正在将 self 与 HASH 和 %args 进行比较
您的原始状态中缺少 ref
。
$self eq 'HASH' && %args
将检查 $self
是否是字符串 'HASH'
。这可能不是你想要的。你想检查 $self
的 reference type,所以你必须做:
ref $self eq 'HASH' && %args
现在它将检查引用的类型,如果是 HASH
,则 return 为真。
你的完整程序代码肯定不是你想要的
'HASH' && %args
字符串'HASH'
始终为真。 %args
将 return %args
的元素数量,因为它是标量上下文,因此如果散列中有任何内容,它将为真。