如何在Perl中检查变量是否具有特定值?是否有命令停止脚本的执行以查找其中的一些变量?
我想知道我是否可以使用Pythonic插入练习:
assert 0, (foo, bar)
以无debuger的方式调试脚本?
答案 0 :(得分:11)
快速CPAN搜索建议Carp::Assert。
答案 1 :(得分:7)
请参阅Carp::Assert:
use Carp::Assert; $next_sunrise_time = sunrise(); # Assert that the sun must rise in the next 24 hours. assert(($next_sunrise_time - time) < 24*60*60) if DEBUG; # Assert that your customer's primary credit card is active affirm { my @cards = @{$customer->credit_cards}; $cards[0]->is_active; };
答案 2 :(得分:5)
Smart::Comments很不错。
答案 3 :(得分:3)
PerlMonks有一个脚本引入了快速断言方法。
速度非常重要,因为Perl被解释,任何内联检查都会影响性能(例如,与简单的C宏不同)
我不确定这些东西是否可以直接使用。
确定!这就是我想要的 - PDF Warning: Test-Tutorial.pdf。 Test::Harness
用于编写Perl模块测试。
答案 4 :(得分:1)
$var_to_check =~ /sometest/ or die "bad variable!";
我倾向于在我的代码中抛出这样的东西,然后使用find和replace来摆脱它们(在生产代码中)。
此外,'eval'可用于运行代码段并捕获错误,并可用于创建异常处理功能。如果你声称某个值不是0,那么你是否想要抛出异常并以特殊方式处理该情况?
答案 5 :(得分:1)
if ( $next_sunrise_time > 24*60*60 ) { warn( "assertion failed" ); } # Assert that the sun must rise in the next 24 hours.
如果您无法访问Carp::Assert所需的Perl 5.9,则可以执行此操作。