如何在 Perl 中进行变量断言?

发布于 2024-07-25 12:59:51 字数 194 浏览 5 评论 0原文

在 Perl 中如何检查变量是否具有特定值? 是否有命令可以停止脚本的执行以查找其某些变量?

我想知道我是否可以使用Pythonic的插入实践:

assert 0, (foo, bar)

以无调试器的方式调试脚本?

How can I check that a variable has a specific value in Perl? Is there a command to stop a script's execution to look up some of its variables?

I wonder if I can use the Pythonic practice of inserting:

assert 0, (foo, bar)

to debug scripts in a debuger-less way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

孤独患者 2024-08-01 13:00:33
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,则可以执行此操作

if ( $next_sunrise_time > 24*60*60 ) { warn( "assertion failed" ); } # Assert that the sun must rise in the next 24 hours.

You can do this if you do not have access to Perl 5.9 which is required for Carp::Assert.

千鲤 2024-08-01 13:00:28
$var_to_check =~ /sometest/ or die "bad variable!";

我倾向于在代码中添加类似的内容,然后使用查找和替换来删除它们(在生产代码中)。

此外,“eval”可用于运行一段代码并捕获错误和可用于创建异常处理功能。 如果您断言某个值不为 0,也许您想抛出异常并以特殊方式处理该情况?

$var_to_check =~ /sometest/ or die "bad variable!";

I tend to throw things like this in my code, and later use a find and replace to get rid of them (in production code).

Also, 'eval' can be used to run a section of code and capture errors and can be used to create exception handling functionality. If you are asserting that a value is not 0, perhaps you want to throw an exception and handle that case in a special way?

败给现实 2024-08-01 13:00:23

PerlMonks 中有一个脚本引入了快速断言方法。

速度很重要,因为 Perl 是解释性的,任何内联检查都会影响性能(例如,与简单的 C 宏不同)


我不确定这些东西是否可以直接使用。


好的! 这就是我正在寻找的 -- PDF 警告:Test-Tutorial.pdf< /a>. Test::Harness 用于编写 Perl 模块测试。

There is a script at PerlMonks that introduces a fast assert method.

Speed is important since Perl is interpreted and any inline checks will impact performance (unlike simple C macros for example)


I am not sure if these things are going to be directly usable.


Ok! This is what i was looking for -- PDF Warning: Test-Tutorial.pdf. The Test::Harness is used for writing Perl module tests.

北城半夏 2024-08-01 13:00:16

请参阅 Carp::Assert

使用 Carp::Assert; 

  $next_sunrise_time = 日出(); 

  # 断言太阳必须在接下来的 24 小时内升起。 
  断言(($next_sunrise_time - 时间) < 24*60*60) if DEBUG; 

  # 断言您客户的主要信用卡处于活动状态 
  肯定{ 
      我的@cards = @{$customer->credit_cards}; 
      $cards[0]->is_active; 
  }; 
  

See 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;
};
演多会厌 2024-08-01 13:00:11

快速 CPAN 搜索建议 Carp::Assert

A quick CPAN search suggests Carp::Assert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文