是否可以在具有“重载”的 Perl 程序中使用 Try::Tiny $SIG{__DIE__}?例如,这个程序的期望输出是“caught it”:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Try::Tiny;
# here how to deal with try and catch
$SIG{__DIE__} =
sub {
print "died my way $_[0]"
};
my $rv;
try {die "its only a flesh wound"; }
catch {
$rv = undef;
print "caught it: $_ ";
};
Run Code Online (Sandbox Code Playgroud)
感谢您的关注和任何建议!
我正在使用现有的 perl 模块,我们称之为 Obj。我向 Obj 添加了一些新功能(子例程/方法),但将它们存储在另一个 .pm 中,称为 Foo。但是我不希望 Obj 继承 Foo 的每个子项。
现在我已经阅读了几个小时的 perl 文档并且很困惑。 https://perldoc.perl.org/Exporter#Selecting-What-to-Export 只是说“不要导出方法名称!”
这是一些示例代码,我不想看到 Obj.pm 中的 sub _not_exported:
#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;
package Foo;# Foo is a new PM file used to extend Obj with collections of subroutine's.
# I want to use hello() from Obj
#use Exporter 'import';
use Exporter qw(import);
our @EXPORT_OK = qw/hello/;
sub hello {
my $self = shift;
$self->{test} = 'Hello world';
}
# I'd rather …Run Code Online (Sandbox Code Playgroud) 为什么三元条件运算符对于未定义的 hashref 键的行为与常规未定义的 $ 变量的行为不同?例如,如果 $form 未定义并且 $str 已定义,则以下三元组始终将 str 的值分配给 form 的值: ($str) ? $form = $str : $form = $form; 然而,如果未定义的哈希键被替换,它就不再起作用。
我制作了一个脚本来演示奇怪的行为。请注意,您将在所有测试中收到“使用未初始化值”警告。这是故意的,因为我正在测试未定义的值。提前致谢
#!/usr/bin/env perl
# ternary operator
# condition ? if True : if False
# Why does the ternary operator behave different with a undefined hashref key
# than a regular undefined $ variable ?
use strict;
use warnings;
use diagnostics;
my $i = 1;
my $form = { vc => 'customer', customer_id => ''};
my $ref …Run Code Online (Sandbox Code Playgroud)