1

私は誰かがいくつかの光を当てることができることを望んでいる問題を抱えています...

私のプログラムには、コードの大部分を含む2つのメインサブルーチンがあり、これらのサブルーチンから、特定のフォルダーの削除、画面への出力など、小さなタスクを実行する他の小さなサブルーチンを呼び出し/参照します..

私の問題の例(説明のために大幅に簡略化されています):

use warnings;
use strict;

sub mainprogram {

    my @foldernames = ("hugefolder", "smallfolder", "giganticfolder");

    SKIP:foreach my $folderName (@foldernames) {
             eval {    
                 $SIG{INT} = sub { interrupt() };     #to catch control-C keyboard command
                 my $results = `grep -R hello $folderName`;  #this takes a long time to grep if its a big folder so pressing control-c will allow the user to skip to the next folder/iteration of the foreach loop
             } 

             print "RESULTS: $results\n";

    }

}

sub interrupt {

     print "You pressed control-c, do you want to Quit or Skip this huge folder and go onto greping the next folder?\n";
     chomp ($quitOrSkip = <STDIN>);
     if ($quitOrSkip =~ /quit/) {
         print "You chose to quit\n";
         exit(0);
     } elsif ($quitOrSkip =~ /skip/) {
         print "You chose to skip this folder and go onto the next folder\n";
         next SKIP;   # <-- this is what causes the problem
     }  else {
         print "Bad answer\n";
         exit(0);
     }

} 

私が抱えている問題

上記のコードでわかるように、バックティック grep コマンドがフォルダーで実行されているときにユーザーがctrl+cを押すと、プログラムを完全に終了するか、配列ループ内の次のフォルダーに移動して grep を開始するかを選択するオプションが与えられます。 .

上記のコードを使用すると、必然的に「次の SKIP のラベルが見つかりません...行...」というエラーが発生します。これは、他のサブルーチンで SKIP ラベルが明らかに見つからないためです。

「次の SKIP」ラベルと「SKIP:foreach」ラベルが異なるサブルーチンにある場合でも、これを行う方法または同じ効果をもたらす何かがあります。つまり、foreach ループの次の反復に進みます。

「次の SKIP」が「SKIP:foreach」と同じブロックにあるように 2 つのサブルーチンを組み合わせることができることは十分承知していますが、プログラムが「割り込み」サブルーチンを何度も何度も呼び出すと、これは、コードの繰り返しが多いことを意味します。

私は非常に明白なことを見落としているかもしれませんが、あなたの助けは大歓迎です、ありがとう

4

1 に答える 1

2

印刷したくない場合はeval、結果の印刷を 内に移動できます。die

foreach my $folderName (@foldernames) {
    eval {    
        local $SIG{INT} = sub { interrupt() };     #to catch control-C keyboard command
        my $results = `grep -R hello $folderName`;  #this takes a long time to grep if its a big folder so pressing control-c will allow the user to skip to the next folder/iteration of the foreach loop
        print "RESULTS: $results\n";
        1;
    } or do {
        # handle the skip if required
    };
}

sub interrupt {
    ...
    die 'skip';
    ...
}

または:

foreach my $folderName (@foldernames) {
    eval {    
        local $SIG{INT} = sub { interrupt() };     #to catch control-C keyboard command
        my $results = `grep -R hello $folderName`;  #this takes a long time to grep if its a big folder so pressing control-c will allow the user to skip to the next folder/iteration of the foreach loop
        1;
    } or do {
        next; # Interrupted (or something went wrong), don't print the result.
    };
    print "RESULTS: $results\n";
}

sub interrupt {
    ...
    die 'skip';
    ...
}
于 2013-08-07T16:20:05.723 に答える