如何在Perl中实现可变断言?

7 投票
6 回答
5484 浏览
提问于 2025-04-15 12:24

我该如何检查一个变量在Perl中是否有特定的值呢?有没有什么命令可以暂停脚本的执行,以便查看一些变量的值?

我在想,是否可以像在Python中那样,插入:

assert 0, (foo, bar)

来以一种不需要调试器的方式调试脚本呢?

6 个回答

5

Smart::Comments 是个很不错的工具。

9

请查看 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;
};
12

快速在CPAN上搜索了一下,发现了一个叫Carp::Assert的东西。

撰写回答