2012-02-17 28 views
0

我知道有一個簡單的單行程或命令可以讓它一遍又一遍地運行,直到我殺死它,有人可以顯示我嗎?我在perl中創建了一個頭部/尾部腳本,並且我希望它可以運行多次

#!/usr/bin/perl 
print "Content-type: text/html\n\n"; 
print "Please type in either heads or tails: "; 
$answer = <STDIN>; 
chomp $answer; 
while ($answer ne "heads" and $answer ne "tails") { 
    print "I asked you to type heads or tails. Please do so: "; 
    $answer = <STDIN>; 
    chomp $answer; 
} 
print "Thanks. You chose $answer.\n"; 
print "Hit enter key to continue: "; 
$_ = <STDIN>; 
if ($answer eq "heads") { 
    print "HEADS! you WON!\n"; 
} else { 
    print "TAILS?! you lost. Try again!\n"; 
} 

是代碼。我想讓它在初始運行後再次提出要求

+0

你能詳細點嗎?你想讓它在同一個文件上反覆運行?是否要確定是否添加了新數據? – kbenson 2012-02-17 01:05:01

+1

#!/ usr/bin/perl print「Content-type:text/html \ n \ n」; 打印「請輸入正面或反面:」; $ answer = ; chomp $ answer; while($ answer ne「heads」and $ answer ne「tails」){ print「我要求你輸入正面或反面,請這樣做:」; $ answer = ; chomp $ answer; } print「Thanks。You choose $ answer。\ n」; 「打回車鍵繼續:」; $ _ = ; if($ answer eq「heads」){ print「HEADS!you WON!\ n」; \ n「; \ n」; \ n「; \ n」; \ n「; } 是代碼,我希望它在初次運行後再次提出要求 – matth0x 2012-02-17 01:05:37

+0

@ matth0x:請編輯您的帖子並將代碼放在那裏,然後刪除您的評論。 – 2012-02-17 01:11:33

回答

1

只需在while循環中包裝代碼的主要部分即可。

#!/usr/bin/perl 
print "Content-type: text/html\n\n"; 
while (1) { 
    print "Please type in either heads or tails: "; 
    $answer = <STDIN>; 
    chomp $answer; 
    while ($answer ne "heads" and $answer ne "tails") { 
     print "I asked you to type heads or tails. Please do so: "; 
     $answer = <STDIN>; 
     chomp $answer; 
    } 
    print "Thanks. You chose $answer.\n"; 
    print "Hit enter key to continue: "; 
    $_ = <STDIN>; 
    if ($answer eq "heads") { 
     print "HEADS! you WON!\n"; 
    } else { 
     print "TAILS?! you lost. Try again!\n"; 
    } 
} 
+0

,如果你把'print'再玩一次?「; chomp(my $ again = <>);如果$ again =〜/ no/i;最後一個大括號的上方,現在可以優雅地退出。 – 2012-02-17 05:53:56

1

很多在這裏的假設,但是從bash shell中「一班輪或命令」,可以用做:

$ while true; do perl yourscript.pl; done 
1

kbenson是正確的,你可以圍繞在無限代碼循環。稍微更優雅的做法是製作一個循環播放的函數,然後圍繞該函數調用進行無限循環。我在這裏使用了一些更多的技巧,其中一些對你來說可能是新的,如果你不明白的地方,請問。另外我同意cjm,我不確定爲什麼內容類型在那裏,所以我把它排除在外。

#!/usr/bin/env perl 

use strict; 
use warnings; 

while (1) { 
    play_round(); 
    print "Would you like to play again?: "; 
    my $answer = <STDIN>; 
    if ($answer =~ /no/i) { 
    print "Thanks for playing!\n"; 
    last; #last ends the loop, since thats the last thing exit would work too 
    } 
} 

sub play_round { 
    print "Please type in either heads or tails: "; 
    my $answer = <STDIN>; 
    chomp $answer; 
    while ($answer ne "heads" and $answer ne "tails") { 
     print "I asked you to type heads or tails. Please do so: "; 
     $answer = <STDIN>; 
     chomp $answer; 
    } 
    print "Thanks. You chose $answer. Now I'll flip.\n"; 

    sleep 1; 

    my @coin = ('heads', 'tails'); 
    my $side = $coin[int rand(2)]; 

    print "And its ... $side! "; 
    if ($answer eq $side) { 
     print "You WON!\n"; 
    } else { 
     print "Sorry, you lost. Try again!\n"; 
    } 
} 
相關問題