perl/open関数〜ファイルを開いて読み込み処理する〜

今期中に会社から求められている成果一覧をもらったので確認していると、ん?と思うことがありました。達成基準の判断指標に、「ブログ(このブログ)を週2回以上更新」とあったのです。ブログの更新が基準になることは知っていたけど、頻度については意識してませんでしたー。あー、更新をがんばらないとなー。


というわけで、しばらくめんどくさすぎて更新が滞っていましたが、本日からまたがんばって更新します。

#! usr/bin/perl

use strict;
use warnings;

open (FILE,'file.txt' ) or die "$!";#「or die "$!"」= 開くのに失敗したらプログラム終了
foreach my $line (<FILE>){
    #chomp ($line);#改行なしで表示されるよ。
     print $line;
}
close (FILE);

=head
#my $lineを省いたもの。$_はデフォルト変数。
open(FILE,'file.txt') or die "$!";
for (<FILE>){
    print "$_";
}
close(FILE);
=cut

上記のプログラムで読み込みに使った「file.txt」は以下。

January 20, 1961 -John F. Kennedy's speech-

We obesrve today not a victory of party, but acelebration of freedom -- symbolizing an end, as well as a beginning -- signifying renewal, as well as change. 
For Ihave sworn before you and Almighty God the same solem oath our forebears prescribed nearly a century and three-puarters ado.

The world is very different now. 
For man holds in his mortal hands the power to abolish all forms of human poberty and all forms of human life. 
And yet the same revolutionary beliefs for which our forebears fought are still at issue around the globe -- the belief that the rights of man come not from the generosity of the state, but from the hand of God.

We dare not forget today that we are the heirs of that first revolution. 
Let the word go forth from this time and place, to freiend and foe alike, that the torch has been passed to a new generation of Americans -- born in this century, tempered by war, disciplined by a hard and bitter peace, proud of our ancient heritage, and unwilling to witness or  permit the slow undoing of those human righats to which this nation has always been committed, and to which we are committed today at home and around the world.

Let ebery nation know, wheter it wishes us well or ill, that we shall pay any price, bear any burden, meet hardship, support any friend, oppose any foe, to assure the survival and the success of liberty.

「FILE」はファイルハンドルと言うらしい。


ちなみにこれは、スピーチがうまいと言われているケネディ元・米大統領の大統領就任演説の一部です。留学していたときに、スピーチが下手で恥ずかしくて研究したため、覚えていました/苦笑。周りが欧米の方ばっかりだったので、余計にスピーチ下手が目立っていたんですよねー。