为什么我应该在Carl中使用Carp而不是警告? [英] Why should I use Carp instead of warn in Perl?

查看:78
本文介绍了为什么我应该在Carl中使用Carp而不是警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们不断给我举鲤鱼的例子,而不是警告.为什么?是什么使鲤鱼比警告更好?

People keep giving me examples with carp instead of warn. Why? What makes carp better than warn?

推荐答案

carp提供了有关消息来自(上下文)的位置的更多信息

carp gives you more info as to where the message comes from (context)

#!/usr/bin/perl

use Carp;

foo();
bar();
baz();

sub foo {
  warn "foo";
}

sub bar {
  carp "bar";
}

sub baz {
  foo();
  bar(); 
}

产生

foo at ./foo.pl line 9.
bar at ./foo.pl line 13
        main::bar() called at ./foo.pl line 6
foo at ./foo.pl line 10.
bar at ./foo.pl line 14
        main::bar() called at ./foo.pl line 19
        main::baz() called at ./foo.pl line 7

对于这个小程序来说,傻瓜很傻,但是当您想知道谁调用了carp鱼的方法时,它就派上用场了.

kinda silly for this small program but comes in handy when you want to know who called the method that's carp'ing.

这篇关于为什么我应该在Carl中使用Carp而不是警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆