我想知道是否有一个模块或编译指示定义了system呼叫返回的信号编号的名称?例如:
use feature qw(say);
use strict;
use warnings;
my $cmd = 'sleep 10';
my $res = system $cmd;
my $signal = $res & 127;
if ( $res == -1 ) {
die "Failed to execute: $!\n";
}
elsif ( $signal == 2 ) {
say " Aborted by user.";
}
elsif ( $signal ) {
printf " Command '%s' Died with signal %d, %s coredump.\n",
$cmd, $signal, ( $? & 128 ) ? 'with' : 'without';
}
Run Code Online (Sandbox Code Playgroud)
而不是 …
我有一个函数func可以返回单个值或值列表,具体取决于函数的调用方式.因此,对于此特定函数,调用者将知道何时仅期望单个返回值,因此希望使用更简单的语法my $var = func( ... )而不是my ($var) = func( ... ).问题是在某些情况下,单个值将转换为具有一个元素的列表/数组的大小,在某些情况下不会.例如:
use feature qw(say);
use strict;
use warnings;
for (1..4) {
no strict 'refs';
my $a = &{"func".$_}();
say $a;
}
sub func1 {
return ("Hi");
}
sub func2 {
my @a = ("Hi");
return @a;
}
sub func3 {
my @a = ("Hi");
return (@a);
}
sub func4 {
my @a = ("Hi");
return (pop @a);
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
Hi
1
1
Hi
Run Code Online (Sandbox Code Playgroud)
为什么在某些情况下和 …
在某些情况下,您需要确定Perl模块的绝对路径名,但不需要加载Perl模块:
use strict;
use warnings;
my $mod_name = 'My::Module';
my $abs_path = mod_name_to_abs_path( $mod_name );
sub mod_name_to_abs_path {
my ( $mod_name ) = @_;
my $rel_fn = $mod_name =~ s{::}{/}gr;
$rel_fn .= '.pm';
require $rel_fn;
return $INC{$rel_fn};
}
Run Code Online (Sandbox Code Playgroud)
上面的代码加载模块(with require).
如何在不使用require的情况下确定模块的绝对路径名?
use feature qw(say);
use strict;
use warnings;
BEGIN {
@INC = qw(dir1 dir2); # <-- lib pragma works fine if I omit this line
# @INC = (); <-- this is what I wanted to do
use lib 'dir3';
}
say for @INC;
Run Code Online (Sandbox Code Playgroud)
输出:
dir1
dir2
Run Code Online (Sandbox Code Playgroud)
预期产量:
dir3
dir1
dir2
Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的测试模块./mods/My/Module.pm6:
unit module My::Module;
use v6;
sub hello () is export {
say "Hello";
}
Run Code Online (Sandbox Code Playgroud)
然后,我有一个测试脚本./p.p6:
#! /usr/bin/env perl6
use v6;
use My::Module;
My::Module::hello();
Run Code Online (Sandbox Code Playgroud)
然后我设置PERL6LIB包含该文件夹./mods,然后运行脚本:
$ ./p.p6
Could not find symbol '&hello'
in block <unit> at ./p.p6 line 7
Run Code Online (Sandbox Code Playgroud)
但是,如果我替换My::Module::hello()脚本中的行,hello()它可以正常工作.我在这里错过了什么?
我正在尝试解析 BibTeX 作者字段,并将其拆分为单独的作者。这将帮助我重写每个作者的姓名首字母。这是一个最小的例子:
use v6;
my $str = '{Rockhold, Mark L and Yarwood, RR and Selker, John S}';
grammar BibTexAuthor {
token TOP {
<all-text>
}
token all-text {
'{' <authors> '}'
}
token authors {
[<author> [' and ' || <?before '}'>]]+
}
token author {
[<-[\s}]> || [' ' <!before 'and '>]]+
}
}
class BibTexAuthor-actions {
method TOP($/) {
say $/;
print "First author = ";
say $<author>.made[0];
make $/.Str;
}
method all-text($/) {
make $/.Str;
} …Run Code Online (Sandbox Code Playgroud) 我有一个命令,期望从管道输入.例如,考虑一下着名的cat命令:
$ echo Hello | cat
Hello
Run Code Online (Sandbox Code Playgroud)
假设我在Perl 6程序中有一个字符串,我想管道输入命令:
use v6;
my $input = 'Hello'; # This is the string I want to pipe to the command.
my $proc = run 'cat', :in($input);
Run Code Online (Sandbox Code Playgroud)
这不起作用(我没有输出).我可以通过调用bash和解决这个问题echo:
my $proc = run 'bash', '-c', "echo $input | cat";
Run Code Online (Sandbox Code Playgroud)
但是,有没有一种方法,我可以做到这一点,但不运行bash和echo?
在Perl5中,我可以简单地做my $pid = open my $fh, '|-', 'cat';,然后print $fh $str.
我想删除a的任何元素SetHash,以便返回其值:
my SetHash $a;
$a<m n k> = 1..*;
my $elem = $a.mymethod;
say $elem; # n
say $a; # SetHash(m k)
Run Code Online (Sandbox Code Playgroud)
我可以分两步完成,如下所示.有没有更好的办法?
my $elem = $a.pick;
$a{$elem}--;
Run Code Online (Sandbox Code Playgroud)
顺便说一下,是否有更多惯用的方法可以添加几个元素SetHash?
以下是否更好?
my SetHash $a;
$a{$_}++ for <m n k>;
Run Code Online (Sandbox Code Playgroud)
要么
my SetHash $a;
$a<m n k> X= True;
Run Code Online (Sandbox Code Playgroud) This file will be foo.pm6:
sub bar { "quux" }
say "Loaded";
Run Code Online (Sandbox Code Playgroud)
And this one requirer.p6:
require "foo.pm6";
say bar;
Run Code Online (Sandbox Code Playgroud)
require fails silently, not loading foo.pm6, and bar is not found. This also fails:
require foo;
say bar;
Run Code Online (Sandbox Code Playgroud)
with the same error, about not finding bar. This file:
require ::"foo";
say bar;
Run Code Online (Sandbox Code Playgroud)
Fails even strangely, with MVMArray: Can't shift from an empty array
UPDATE: it fails silently because it stops when it finds an unknown symbol, bar, …
我正在尝试查找 crypto 函数定义的 C 源代码PEM_read_bio_X509()。例如,请参阅C++ 中的此应用程序。我认为这将是openssl 源代码的一部分。但如果我这样做:
$ git clone git@github.com:openssl/openssl.git
$ cd openssl
$ find . -type f -exec grep -I -Hn --color=auto PEM_read_bio_X509 {} \;
Run Code Online (Sandbox Code Playgroud)
我得到了一些匹配项,但只是用例,而不是函数的定义。
我还可以生成共享库(libcrypto.so):
$ ./config
$ make
$ nm -g ./libcrypto.so | grep PEM_read_bio_X509
00000000001cb050 T PEM_read_bio_X509
00000000001cb0f0 T PEM_read_bio_X509_AUX
00000000001c6070 T PEM_read_bio_X509_CRL
00000000001c5f70 T PEM_read_bio_X509_REQ
Run Code Online (Sandbox Code Playgroud)
所以该符号PEM_read_bio_X509确实是在生成的libcrypto.so. 我缺少什么?