Perl:为什么 Term::Readline 会破坏 STDIN/STDOUT?

发布于 2024-12-26 04:46:22 字数 573 浏览 3 评论 0原文

如果我创建新的 Term::Readline 对象(并传递 binmoded STDIN 和 STDOUT),输入和输出将被模拟。示例:

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;
use utf8::all;
use Term::ReadLine;

my $X = <>;
chomp $X;
say "õ $X";

my $term = new Term::ReadLine ('test', \*STDIN, \*STDOUT);

say "õ $X";
$X = <>;
chomp $X;
say "õ $X";

__END__
got:
õ
õ õ
� �
õ
� õ

如果我输入拉丁语 1 中不存在的字符,我会收到“宽字符”警告,并且输出更好,但仍然不正确:

ž
õ ž
Wide character in print at db.test.pl line 20, <> line 1.
õ ž
ž
� ž

使用 Term::Readline 时如何正确设置 IO?

If I create new Term::Readline object (and pass binmoded STDIN and STDOUT), input and output get mocked. Example:

#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;
use utf8::all;
use Term::ReadLine;

my $X = <>;
chomp $X;
say "õ $X";

my $term = new Term::ReadLine ('test', \*STDIN, \*STDOUT);

say "õ $X";
$X = <>;
chomp $X;
say "õ $X";

__END__
got:
õ
õ õ
� �
õ
� õ

If i input characters, which are not present in Latin 1, i get "Wide character" warning and output is better, but still not correct:

ž
õ ž
Wide character in print at db.test.pl line 20, <> line 1.
õ ž
ž
� ž

How properly set IO when using Term::Readline?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2025-01-02 04:46:22

构造函数将“stdio”层添加到需要删除的流中。请参阅 PerlIO 中的 :pop

#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use utf8::all;
use Devel::Peek;
use Term::ReadLine;

my $X = <>;
Dump $X;
chomp $X;
say "õ $X";

say join ' ', PerlIO::get_layers('STDIN');
say join ' ', PerlIO::get_layers('STDOUT');

my $term = new Term::ReadLine ('test', \*STDIN, \*STDOUT);

say join ' ', PerlIO::get_layers('STDIN');
say join ' ', PerlIO::get_layers('STDOUT');

binmode 'STDIN', ':pop';
binmode 'STDOUT', ':pop';

say join ' ', PerlIO::get_layers('STDIN');
say join ' ', PerlIO::get_layers('STDOUT');

say "õ $X";
$X = <>;
Dump $X;
chomp $X;
say "õ $X";

__END__
ž
õ ž
SV = PV(0x753160) at 0x778968
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK,UTF8)
  PV = 0x766830 "\305\276"\0 [UTF8 "\x{17e}"]
  CUR = 2
  LEN = 80
unix perlio encoding(utf-8-strict) utf8
unix perlio encoding(utf-8-strict) utf8
unix perlio encoding(utf-8-strict) utf8 stdio
unix perlio encoding(utf-8-strict) utf8 stdio
unix perlio encoding(utf-8-strict) utf8
unix perlio encoding(utf-8-strict) utf8
õ ž
ž
SV = PV(0x753160) at 0x778968
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK,UTF8)
  PV = 0x766830 "\305\276\n"\0 [UTF8 "\x{17e}\n"]
  CUR = 3
  LEN = 80
õ ž

The constructor adds the 'stdio' layer to the streams which you need to remove. See :pop in PerlIO.

#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use utf8::all;
use Devel::Peek;
use Term::ReadLine;

my $X = <>;
Dump $X;
chomp $X;
say "õ $X";

say join ' ', PerlIO::get_layers('STDIN');
say join ' ', PerlIO::get_layers('STDOUT');

my $term = new Term::ReadLine ('test', \*STDIN, \*STDOUT);

say join ' ', PerlIO::get_layers('STDIN');
say join ' ', PerlIO::get_layers('STDOUT');

binmode 'STDIN', ':pop';
binmode 'STDOUT', ':pop';

say join ' ', PerlIO::get_layers('STDIN');
say join ' ', PerlIO::get_layers('STDOUT');

say "õ $X";
$X = <>;
Dump $X;
chomp $X;
say "õ $X";

__END__
ž
õ ž
SV = PV(0x753160) at 0x778968
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK,UTF8)
  PV = 0x766830 "\305\276"\0 [UTF8 "\x{17e}"]
  CUR = 2
  LEN = 80
unix perlio encoding(utf-8-strict) utf8
unix perlio encoding(utf-8-strict) utf8
unix perlio encoding(utf-8-strict) utf8 stdio
unix perlio encoding(utf-8-strict) utf8 stdio
unix perlio encoding(utf-8-strict) utf8
unix perlio encoding(utf-8-strict) utf8
õ ž
ž
SV = PV(0x753160) at 0x778968
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK,UTF8)
  PV = 0x766830 "\305\276\n"\0 [UTF8 "\x{17e}\n"]
  CUR = 3
  LEN = 80
õ ž
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文