perl/while文、foreach文、last、next、redo、二項演算子〜いろいろな繰り返し処理〜

実験をたくさんしてみた。読み込みファイルは、下の方に書いています。

#! usr/bin/perl

use strict;
use warnings;

#last =>現在のループを終了する

=head
open(FILE,'file.txt') or die"$!";
while(<FILE>){
    #chomp;
#ファイルの中身の先頭が数字以外のとき、中身をprintせずにファイルを閉じる。
    if ($_ =~ /^\D/){
        last;
    }
    print $_;
}
close (FILE);
=cut

=head
#if修飾子を使ってみた。
open (FILE,'file.txt') or die "$!";
while(<FILE>){
    last if($_ =~ /^\D/);
    print "$_";
}
close (FILE);
=cut

=head
#同じくif修飾子を使ってみた。
open(FILE,'file.txt') or die "$!";
while(<FILE>){
    last if($_ =~ /^\d/);
    print "$_";
}
close (FILE);
=cut

=head
open(FILE,'file.txt') or die "$!";
while(<FILE>){
#開いたファイルの中身の先頭に数字がきていたら中身をprintせずにファイルを閉じる
    if($_ =~ /^\d/){
        last;
        }
    print $_;
    }
close(FILE);
=cut

my @file = ('6_6_1.txt','6_6_2.txt','6_6_3.txt');

=head
#二重ループでlastを使ってみた。=>lastが入っているループのみ終了
foreach (@file){
    open(FILE,"$_") or die "$!";
    while(<FILE>){
        last if ($_=~ /^w/); #最初の文字がwのとき
        print "$_\n\n";
    }
    close(FILE);
}
=cut

#ラベルをつけるとラベルのついた部分までとぶ。
outer: foreach(@file){#「outer」がラベル
    open(FILE,"$_") or die "$!";
    while(<FILE>){
        last outer if ($_=~ /^w/);
        last if ($_=~ /^n/);
        print "$_";
    }
    close (FILE);
    print "123456\n";
}
print "you has jumped last line!\n";
print "\n";

=head
#next=>繰り返しを次ぎに進める。ループを抜け出すわけではなく、条件式の評価に戻る。
#「a」が一番最初にくる行以外をprint
open(FILE,'6_6_4.txt') or die "$!";
while (<FILE>){
    next if ($_=~ /^a/);#一番最初の文字が「a」であれば、print分を実行せずにwhileに戻る
    print "$_";
}
close (FILE);
=cut

=head
#無限ループになります。shellを閉じて終わらせてください。
open(FILE,'6_6_4.txt') or die "$!";
while (<FILE>){
    if ($_=~ /^a/){
        print "$_\n";#「apple」がたくさん出続けます。
        redo;
    }
    if ($_=~ /^[^a]/){
        print "$_\n";
    }
}
close(FILE);
=cut

open (FILE,'6_6_4_1.txt') or die "$!";
while(my $line =<FILE>){
    chomp($line);
    if($line =~ /---$/){
        $line .=<FILE>;#二項演算子を使用
        redo;
    }
    print $line,"\n";
}
close(FILE);

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.

6_6_1.txt

hello everyone!

6_6_2.txt

nice to meet you!

6_6_3.txt

why don't you join us?

6_6_4.txt

hello
come
happy
apple
visit
laugh
aim
evening
girl
ask
attack
achive
shame
be
touch
atempt
attention
test
finish

6_6_4_1.txt

Twinkle, twinkle, litle star,
How I wonder what you are.
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are.

二項演算子について。二項演算子とは、両辺に同じ変数を使用した計算を行うときに使う簡略表記のこと。

元の式 二項演算子
$a=$a+$b $a +=$b
$a=$a-$b $a -=$b
$a=$a*$b $a *=$b
$a=$a/$b $a /=$b
$a=$a.$b $a .=$b
$a=$aX$b $a X=$b