当用户按下时,如何读取Term::ReadLine用户输入而不打印换行符Enter?
我想要这样做的原因是因为我想从屏幕最底部的提示中读取用户输入(如 或 中less)vim。目前,按下Enter会导致屏幕向下滚动,这可能是一个问题。ncurses另外,我想避免在这一点上呼吁。
设置
$term->Attribs->{echo_control_characters}为0、undef、 或off似乎不起作用。
#!perl
use Term::ReadLine;
use Term::ReadKey;
my $term = new Term::ReadLine ('me');
$term->ornaments(0);
$term->Attribs->{echo_control_characters} = 0;
print STDERR "\e[2J\e[s\e[" . ( ( GetTerminalSize() ) [1] ) . ";1H"; # clear screen, save cursor position, and go to the bottom;
my $input = $term->readline('> ');
print STDOUT "\e[uinput = $input\n"; # restore cursor position, and print …Run Code Online (Sandbox Code Playgroud) Perl 的类引号运算符从裸字qw()创建单词列表,而方括号 []可用于创建对匿名数组的引用。现在,我想知道 Perl 是否提供了一种以某种方式缩写的方法:
my $aref = [qw( a b c )];
Run Code Online (Sandbox Code Playgroud)
使用类似不存在的 qa()运算符之类的东西:
my $aref = qa( a b c );
Run Code Online (Sandbox Code Playgroud)
我最近经常将qw()和一起使用,我想要的只是减少混乱。[]
注意:这不是我要找的:
my @a = \( qw( a b c ) );
Run Code Online (Sandbox Code Playgroud) 如何vim有条件地映射序列以运行两个外部程序中的任何一个,以便屏幕不会被清除以显示该else子句?
例如:
:nmap <c-l> :if filereadable('Makefile')<CR>!make<CR>else<CR>!ls<CR>endif<CR>
Run Code Online (Sandbox Code Playgroud)
ctrl+ m执行make但随后清除屏幕并在其底部打印以下内容:
: else
: !ls
: endif
Press ENTER or type command to continue
Run Code Online (Sandbox Code Playgroud) 如何递归匹配与多字符分隔符平衡的字符串?
考虑一个LaTeX内联引号,这样2个doubleticks(``)标记引号开始的位置,2个撇号(\x27\x27)标记结束.
以下代码给了我``five''.我想抓住two ``three `four' ``five'' three four'' six
my $str = q|one ``two ``three `four' ``five'' three four'' six'' seven|;
if ( $str =~ /
(
``
(?:
[^`']
|
(?1)
)*
''
)
/x
)
{
print "$1\n";
}
Run Code Online (Sandbox Code Playgroud)
我想它与如何否定有关,而不是字符类([^`']但是多字符串.
什么是删除目录的Perl方法,然后是所有空的 父目录,直到第一个非空目录?换句话说,可以使用什么而不是:
system 'rmdir', '-p', $directory;
Run Code Online (Sandbox Code Playgroud)
哪个,d首先删除d,然后c,然后b,但不是a,因为a仍然包含x,像这样:
a
a/b
a/b/c
a/b/c/d
a/x
Run Code Online (Sandbox Code Playgroud)
导致
a
a/x
Run Code Online (Sandbox Code Playgroud)
这不是内置的rmdir,因为它只能删除一个单一的目录.(doc)
它不是finddepth ( sub {rmdir}, '.' ),使用File::Find,因为它删除了孩子,而不是父母.(doc)
这也不是模块的remove_tree功能File::Path,因为它不仅删除子目录而且删除文件.(doc)
注意,remove_tree并finddepth在Bash的相反方向工作rmdir --parent.
如何捕获整数与其后继之间的任何内容?例如,假设我们要抓住b3c,这是间1和2在a1b3c2d.下面的代码给了我们b3c2d.
'a1b3c2d' =~ / (\d+) (.+) (?{ $1 + 1 }) /x;
print $2, "\n";
Run Code Online (Sandbox Code Playgroud) Perl方法通常以my $self = shift;并且随后用于$self引用它是方法的对象开始.
sub foo {
my $self = shift;
$self->method;
...
return $self->{_foo}
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是$_[0]直接使用而不是复制$_[0]到 $self:
sub foo {
$_[0]->method;
...
return $_[0]->{_foo}
}
Run Code Online (Sandbox Code Playgroud)
现在,什么更有效(更快):
将内存分配给一个新变量$self,获取列表的第一项@_,并在每次调用一个方法时将该值复制到该变量;
每次使用时获取数组的第一项$_[0]?