Perl 板


LINE

※ 引述《herolee (hero)》之铭言: : ※ 引述《frank1983 (What?)》之铭言: : : 您可以使用 open STDERR, ">&STDOUT" : : (请参考 http://perldoc.perl.org/functions/open.html ) : : CPAN 上有一些 module 可以试试: File::Tee (不能在 Windows 上执行)、Tee、 : : IO::CaptureOutput 等 : : 这些 module 都能将如 system() 的输出也导到档案 (跟 tee 的功能相同) : : 如果您只是想将 print、printf、syswrite 等输出的结果同时导到档案,而忽略 : : system() 等由子行程输出的结果 (这可能需要透过 IPC) : : 则可以考虑使用 IO::Tee : : 您甚至可以自己利用 tie 实作一简单的多工输出的 file handle (事实上 IO::Tee 就是 : : 用 tie 实作的) : : Tee.pm 的内容为: : : #!/usr/bin/perl : : use warnings; : : use strict; : : package Tee; : : sub TIEHANDLE { : : my $class = shift; : : my @fh = @_; : : bless \@fh, $class; : : } : : sub WRITE { : : my $this = shift; : : syswrite $_, @_ for @$this; : : } : : sub PRINT { : : my $this = shift; : : print {$_} @_ for @$this; : : } : : sub PRINTF { : : my $this = shift; : : printf {$_} @_ for @$this; : : } : : 1; : : 而主程式为: : : #!/usr/bin/perl : : use warnings; : : use strict; : : use Tee; : : open my $log, '>', 'log.txt' or die; : : tie *FH, 'Tee', \*STDOUT, $log; : : select(*FH); : : print "hello world!\n"; : 如前所述﹐还有两点不明﹐希望指点。 : 1、这个例子并没有重定向STDERR﹐有办法收集到STDERR的东东麽。 : 另外诸如die "Error:$!"这种有没有办法实现最初的设想﹐类似2>&1 |tee这种效果。 其实 select(*FH) 的作用就是将 FH 做为 write 和 print 预设的 file handle 所以 print "hello world!" 会将 "hello world!" 输出至 FH 但是 print STDOUT "hello world!" 或 print STDERR "hello world!" 其实还是会将 "hello world!" 直接输出至 STDOUT 和 STDERR 而不会透过 FH 如果想收集 die "Error: $!",你也许可以定义 $SIG{__DIE__} 这个 signal handler local $SIG{__DIE__} = sub { print {$log} @_; }; 请参考: 1. http://perldoc.perl.org/functions/select.html 2. http://perldoc.perl.org/functions/die.html : 2、系统调用比如 `ls -l. `之类不能达到上述效果。 : 可以通过print $tee `ls -l .`;来实现﹐但是这样的效果是屏幕输出不及时。 : 得等系统命令执行完毕後﹐才能打印输出。这对於一些执行时间较长的命令﹐ : 效果很不好。 : : 这时 "hello world!\n" 会同时输出到 STDOUT 和档案 log.txt : : (请参考 http://perldoc.perl.org/perltie.html#Tying-FileHandles ) print $tee `ls -l .`; 不太可能实现即时输出 毕竟它就是将 `ls -l` 的结果先存下来再输出至 $tee 你如果想实现即时输出可能需要使用 IPC 你可以参考 http://perldoc.perl.org/perlipc.html#Using-open%28%29-for-IPC 当然,最简单且比较不容易出错的方式还是使用现成的套件: 如之前提到的 1. File::Tee : http://search.cpan.org/dist/File-Tee/ 2. Tee : http://search.cpan.org/dist/Tee/ 3. IO::CaptureOutput : http://search.cpan.org/dist/IO-CaptureOutput/ 这些套件应该都能符合你的要求 如果我有任何理解错误也希望大大们能给予指教~ --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 111.243.162.213







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:iOS站内搜寻

TOP