![](/img/trans.png)
[英]How to pipe stdin into a perl script that is looking for input as the only parameter?
[英]How to read password line after pipe input to Perl script
我正在嘗試讀取STDIN
然后獲取用戶輸入行而不在終端中顯示它。
帶有ReadMode('noecho')
的Term::ReadKey
解決方案不起作用,因為它使用<STDIN>
並且如果它不為空它立即采取(應該是文件,例如管道數據)作為輸入而不是實際上有效:
use warnings;
use strict;
use Term::ReadKey;
my $_stdin = <STDIN>;
print "Enter your password:\n";
ReadMode('noecho');
my $_pass = ReadLine(0); # This one uses <STDIN>!
ReadMode(0);
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";
輸出:
$ echo "some data" | perl term-readkey.pl
Enter your password:
Use of uninitialized value $_pass in concatenation (.) or string at term-readkey.pl line 10, <STDIN> line 1.
STDIN:
some data
Password:
我遇到的唯一解決方案是使用Term::ReadLine
,它似乎沒有使用<STDIN>
作為Term::ReadKey
,但問題是$_term->readline()
是可見的:
use warnings;
use strict;
use Term::ReadLine;
my $_stdin = <STDIN>;
my $_term = Term::ReadLine->new('term');
my $_pass = $_term->readline("Enter your password:\n");
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";
輸出:
$ echo "some data" | perl term-readkey.pl
Enter your password:
25 # actually entered it, and its visible...
STDIN:
some data
Password:
25
有一個類似的問題 ,但答案只適用於Unix系統,輸入是可見的......
所以我找到了解決方案,這非常簡單:
使用Term::ReadKey
的ReadMode和Term::ReadLine
的術語IN
,例如:
use Term::ReadLine;
use Term::ReadKey;
my $_stdin = <STDIN>;
my $_term = Term::ReadLine->new('term');
ReadMode('noecho', $_term->IN);
my $_pass = $_term->readline("Enter your password:\n");
ReadMode(0, $_term->IN);
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";
或者(感謝Ujin )
use Term::ReadLine;
use Term::ReadKey;
my $_stdin = <STDIN>;
my $term = Term::ReadLine->new('term');
my @_IO = $term->findConsole();
my $_IN = $_IO[0];
print "INPUT is: $_IN\n";
open TTY, '<', $_IN;
print "Enter your password:\n";
ReadMode('noecho', TTY);
my $_pass = <TTY>;
ReadMode(0, TTY);
close TTY;
print "STDIN:\n$_stdin\nPassword:\n$_pass\n";
輸出:
Enter your password:
# here enter hiddenly
STDIN:
stdin input
Password:
paws
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.