如何将die函数的输出重定向到Perl中的文件? [英] How can I redirect output of die function to a file in Perl?

查看:105
本文介绍了如何将die函数的输出重定向到Perl中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将die消息重定向到一个单独的文件,以便以后可以比较该文件以确定出了什么问题.

I want to redirect the die messages to a separate file so that I can compare that file later to determine what went wrong.

但是这段代码给了我错误:

But this code gives me errors:

$ cat test.pl
use strict;
use warnings;

my $log = "msglog.log";
die $log "DEAD$!";

$ perl test.pl
Missing comma after first argument to die function at test.pl line 5, near ""DEAD$!";"
Execution of test.pl aborted due to compilation errors.
$ 

我不想从呼叫者处执行2>.是否可以通过脚本将它们重定向?

I do not want to do a 2> from the caller. Is there someway to redirect them from within the script?

推荐答案

Perl的die打印到STDERR,因此您可以将STDERR重定向到文件.

Perl's die prints to STDERR so you could redirect STDERR to a file.

#!/usr/bin/env perl
# the path above depends on your system

open(STDERR, ">>", "errlog.log");
die "Hello";

这篇关于如何将die函数的输出重定向到Perl中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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