開発環境
- 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.
コード(TextWrangler)
#!/usr/bin/env perl
use strict;
use warnings;
use 5.012;
use IO::Tee;
my $fh;
my $scalar;
my $out;
while(1){
print "出力先を選択(File, Scalar, Both): ";
$out = <STDIN>;
given($out){
when(/^F/){open $fh, ">>","tmp_file";}
when(/^S/){open $fh, ">>",\$scalar;}
when(/^B/){
open my $file_fh,">>","tmp_file";
open my $scalar_fh,">>",\$scalar;
$fh = IO::Tee->new($file_fh, $scalar_fh);
}
when(/^\s*$/){ exit;}
default {
print "入力内容をを確認してください\n";
next;
}
}
last;
}
my $day = (localtime)[3];
my $wday = (localtime)[6];
given($wday){
when(0){$wday = "日";}
when(1){$wday = "月";}
when(2){$wday = "火";}
when(3){$wday = "水";}
when(4){$wday = "木";}
when(5){$wday = "金";}
when(6){$wday = "土";}
}
print {$fh} "日付: $day日, 曜日: $wday曜日\n";
print STDOUT "$scalar" if $out =~/^[SB]/;
入出力結果(Terminal)
$ ./sample.pl 出力先を選択(File, Scalar, Both): a 入力内容をを確認してください 出力先を選択(File, Scalar, Both): Scalar 日付: 20日, 曜日: 金曜日 $ ./sample.pl 出力先を選択(File, Scalar, Both): File $ cat tmp_file 日付: 20日, 曜日: 金曜日 $ rm tmp_file $ ./sample.pl 出力先を選択(File, Scalar, Both): Both 日付: 20日, 曜日: 金曜日 $ cat tmp_file 日付: 20日, 曜日: 金曜日 $ ./sample.pl 出力先を選択(File, Scalar, Both): $
0 コメント:
コメントを投稿