遠離Perl一會兒,並且想修改我很早以前寫作藝術項目的腳本。原始腳本使用Term :: ReadKey允許用戶在Mac/Linux終端中輸入任意文本。當他們鍵入時,文本會在終端中創建各種浮動模式。 我想修改腳本,而不是在輸入鍵時讀取鍵,它可以從另一個進程定期寫入的文本文件中讀取。但是它需要以某種可控的方式(不是一次全部)讀取字符,以便(粗略地)模擬人類打字。Perl術語:: ReadKey - 從文件中讀取,好像它正在輸入
我已經試過: 期限:: ReadKey的手冊頁說,它可以從一個文件句柄,而不是標準輸入讀 - 但由於某些原因,我不能得到這個工作,用一個標準的文件或一個FIFO。我還嘗試使用「打開」從文件中讀取文本,並將這些字符放入數組中。但是迭代遍歷數組會變得複雜,因爲需要在字符之間添加延遲而不暫停腳本的其餘部分。 (我可以設想這是一個潛在的解決方案,但我不知道如何設計它,以便延遲時間可以控制,而不會使腳本變得笨拙。)
想知道是否有相對簡單的方法來處理 - 假設它是可行的?
這裏的現有腳本(已刪除了基於各種按鍵添加額外效果的各種子程序。)
#!/usr/bin/perl
use Time::HiRes(usleep);
use Term::ReadKey;
$|=1;
$starttime = time;
$startphrase = ' ';
$startsleepval = 3000;
$phrase = $startphrase;
$sleepval = $startsleepval;
$dosleep = 1;
$SIG{'INT'}=\&quitsub;
$SIG{'QUIT'}=\&quitsub;
# One Ctrl-C clears text and resets program. # Three Ctrl-C's to quit.
sub quitsub {print color 'reset' if ($dosleep); $phrase = $startphrase; $sleepval=$startsleepval; $SIG{'INT'}=\&secondhit;}
sub secondhit { $SIG{'INT'}=\&outtahere; }
sub outtahere {print color 'reset'; sleep 1; print "\n\n\t\t\t\n\n"; exit(0);}
while (1) {
print "$phrase ";
if ($dosleep) {
usleep ($sleepval);
}
ReadMode 3;
##### Here is where it reads from the terminal. Can characters be read from a file in a similar sequential fashion? #####
$key = ReadKey(-1);
$now = time;
if ((defined($key)) and ($now > $starttime + 5)) {
$phrase = $phrase.$key;
$SIG{'INT'}=\&quitsub;
}
# user can also create interesting effects with spacebar, tab and arrow keys.
ReadMode 0; # this may appear redundant, but has a subtle visual effect. At least that's what I commented in the original 2003 script.
}
# end main loop
有趣的想法,但我沒有得到任何工作。 File :: Tail似乎沒有幫助解決時序問題。嘗試了所有的Term :: ReadKey建議,但問題不在於這些角色一次吐出來;當我從文件夾中讀取字符時,根本不會吐出。嘗試正常的文件以及fifo。 – artistwhocodes