開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9) の8章(ファイルハンドルへのリファレンス), 8.6(練習問題)1を解いてみる。
1.
やり方の1つ。(「やり方は何通りもある」(TIMTOWTDI(There Is More Than One Way To Do It.)))
コード(TextWrangler)
#!/usr/bin/env perl use strict; use warnings; use IO::Tee; my $f; my $s; my $mode; while(1){ print "出力先(ファイル(f)、スカラー(s)、両方同時(b)を入力: "; chomp($mode =); if($mode =~ /^f/){ open $f, ">>", 'date.txt'; last; } elsif ($mode =~/^s/){ open $f, ">>", \$s; last; } elsif ($mode =~ /^b/){ open my $bs, ">>", \$s; open my $bf, ">>", "date.txt"; $f = IO::Tee->new($bs,$bf); last; } else { print "入力内容を確認してください\n"; } } my $date = localtime; print $f "$date\n"; print STDOUT "$s" if $mode =~ /^[sb]/;
入出力結果(Terminal)
$ ./sample.pl 出力先(ファイル(f)、スカラー(s)、両方同時(b)を入力: a 入力内容を確認してください 出力先(ファイル(f)、スカラー(s)、両方同時(b)を入力: f $ cat date.txt Tue Apr 24 17:17:58 2012 $ ./sample.pl 出力先(ファイル(f)、スカラー(s)、両方同時(b)を入力: s Tue Apr 24 17:18:18 2012 $ cat date.txt Tue Apr 24 17:17:58 2012 $ ./sample.pl 出力先(ファイル(f)、スカラー(s)、両方同時(b)を入力: b Tue Apr 24 17:18:42 2012 $ cat date.txt Tue Apr 24 17:17:58 2012 Tue Apr 24 17:18:42 2012 $
0 コメント:
コメントを投稿