小编U. *_*ndl的帖子

autoflush问题

我在一段代码中找到了类似的东西:

use IO::Handle;
autoflush STDOUT 1;
print '';
Run Code Online (Sandbox Code Playgroud)

"打印"的目的是清空可能填充的缓冲区吗?

perl buffer flush

3
推荐指数
1
解决办法
2888
查看次数

如何解析未知长度的二进制 PDF 流?

来自 PDF 文档:“流字典后面的关键字流应后跟行尾标记,该标记由 CARRIAGE RETURN 和 LINE FEED 或仅 LINE FEED 组成,而不是单独的 CARRIAGE RETURN。组成流的字节序列位于流关键字后面的行尾标记和 endstream 关​​键字之间;流字典指定了确切的字节数。

由于内容可能是二进制的,因此endstream的出现不一定表示流的结束。现在考虑这个流时:

%PDF-1.4
%307?
5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
stream
x234+T03203T0^@A(235234?^_d256220^314^U310^E^@[364^F!endstream
endobj
6 0 obj
30
endobj
Run Code Online (Sandbox Code Playgroud)

长度是一个间接对象如下的流。显然,该长度只能在解析流才能读取。

我认为让 Length 成为只能在流才能解决的间接对象是设计缺陷。虽然它可以帮助 PDF 编写者按顺序输出 PDF,但它使 PDF 阅读器的解析变得非常困难。考虑到 PDF 文件的阅读频率高于写入频率,我不明白这一点。

那么如何正确解析这样的流呢?

pdf parsing stream

3
推荐指数
1
解决办法
1370
查看次数

测试数组中 `undef` 与“空槽”的差异

我有一个数据结构,它具有一个带有预分配插槽(“空插槽”)的数组。编写打印例程,我想知道如何区分“空槽”和undef有价值的槽。Perl 调试器可以做到这一点,但我不知道它如何检测差异。

例子:

  DB<10> $r = []
  DB<11> $#$r=4
  DB<12> $r->[4]=undef
  DB<13> x $r
0  ARRAY(0x55d8fa6797e8)
   0  empty slot
   1  empty slot
   2  empty slot
   3  empty slot
   4  undef
Run Code Online (Sandbox Code Playgroud)

arrays perl undefined

3
推荐指数
1
解决办法
173
查看次数

`print $fh &lt;&lt;'EOF'` 中的 here-doc 有问题:Perl 执行 here doc

(根据/sf/answers/1223568601/它应该可以工作,但不能)我有一些这样的代码:

use strict;
use warnings;
if (open(my $fh, '>', '/tmp/test')) {
    print $fh << 'TAG';
    BEGIN {
       something;
    }
TAG
    close($fh);
}
Run Code Online (Sandbox Code Playgroud)

如果我省略$fh(这是一个为输出而打开的文件句柄,顺便说一句),该BEGIN块将正确输出(到STDOUT)。但是,当我添加时$fh,Perl (5.18, 5.26) 尝试执行something导致运行时错误:

Bareword "something" not allowed while "strict subs" in use at /tmp/heredoc2.pl line 6.
syntax error at /tmp/heredoc2.pl line 9, near "FOO
    close"
Execution of /tmp/heredoc2.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)

怎么了?

perl

3
推荐指数
1
解决办法
105
查看次数

为什么我只得到第一个捕获组?

/sf/answers/161323851//sf/answers/2590295011/没有帮助我)

/proc/stat在分析 Linux 中的问题时,我开始编写一个小实用程序,但是我无法按照我想要的方式获取捕获组。这是代码:

#!/usr/bin/perl
use strict;
use warnings;

if (open(my $fh, '<', my $file = '/proc/stat')) {
    while (<$fh>) {
        if (my ($cpu, @vals) = /^cpu(\d*)(?:\s+(\d+))+$/) {
            print "$cpu $#vals\n";
        }
    }
    close($fh);
} else {
    die "$file: $!\n";
}
Run Code Online (Sandbox Code Playgroud)

例如,使用这些输入行,我得到输出:

> cat /proc/stat
cpu  2709779 13999 551920 11622773 135610 0 194680 0 0 0
cpu0 677679 3082 124900 11507188 134042 0 164081 0 0 0
cpu1 775182 3866 147044 38910 135 0 15026 …
Run Code Online (Sandbox Code Playgroud)

regex perl regex-group

3
推荐指数
1
解决办法
191
查看次数

/:$foovar/ 这在 Perl 中起什么作用?

我正在查看 Perl 脚本来了解它的作用。

我遇到了这个

if (/:$array[k]/)
Run Code Online (Sandbox Code Playgroud)

/:$something/Perl是做什么的?

regex perl

3
推荐指数
1
解决办法
87
查看次数

“使用版本”下的“v1.2.3”和“qv(1.2.3)”有什么区别?

尝试将版本号与多个点进行比较,我想知道该怎么做:

Perl 有一个version模块似乎可以完成这项工作,但我想知道:

  DB<1> use version

  DB<2>  x '1.2.3' > '1.2.29'
0  ''
  DB<3>  x '1.2.3' >= '1.2.29'
0  1
  DB<4>  x '1.2.3' cmp '1.2.29'
0  1
  DB<5>  x '1.2.3' cmp '1.2.29'
0  1
  DB<6> x qv(1.2.3)
0  v1.2.3
  DB<7> x qv(1.2.3) > qv(1.2.29)
0  ''
  DB<8> x qv(1.2.3) < qv(1.2.29)
0  1
  DB<9> x '1.2.3' == '1.2.29'
0  1
  DB<10> x v1.2.3
0  "\cA\cB\cC"
  DB<11> print v1.2.3

  DB<12> print qv(1.2.3)
v1.2.3
Run Code Online (Sandbox Code Playgroud)

Soqv(1.2.3)被转换为v1.2.3,但是当我打印时v1.2.3 …

perl version

3
推荐指数
1
解决办法
97
查看次数

结合 `foreach` 和 `if` 的 Perl 语法:它不应该工作,即:为什么它不工作?

试图在不能包含负数的数组中获得最大值我试过:

my @v;
#...
my $max = 0;
$max = $_
    if ($_ > $max)
        foreach (@v);
Run Code Online (Sandbox Code Playgroud)

perl 5.18.2 出现语法错误。

但是 (1)statement($_) foreach (@v);和 (2)$max = $_ if ($_ > $max);都可以,并且它们做了它们应该做的事情。

因此,如果 (1) 和 (2) 都是有效的语句,为什么不能将它们组合在用于 (1) 的模式中?

只是没有人需要提出建议;这是我使用不同语法解决问题的方法:

foreach (@v) {
    $max = $_
        if ($_ > $max);
}
Run Code Online (Sandbox Code Playgroud)

arrays syntax perl for-loop max

2
推荐指数
1
解决办法
160
查看次数

如何通过“use strict”正确使用“我们的”类变量和“UNITCHECK”?

由于 Perl 常量使用起来有些奇怪,我决定将我的“类变量”实现为our变量,就像:

our $foo = '...';
Run Code Online (Sandbox Code Playgroud)

但是,当我UNITCHECK使用类变量添加块时,我意识到变量尚未设置,因此我将代码更改为:

BEGIN {
   our $foo = '...';
}

UNITCHECK {
    if ($foo eq 'bla') {
        #...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我意识到我在 中输入了一些变量名称UNITCHECK,所以我决定添加use warningsuse strict。不幸的是我遇到了新的错误,比如

变量“$foo”未在 .. 行导入。

当我初始化外部变量时BEGIN,错误就消失了,但随后又回到了原来的问题。

所以我想知道:是our $var = 'value';推荐的正确使用,还是应该分为our $var;外部BEGIN$var = 'value;内部BEGIN

由于我的变量列表相当长,我试图避免将它们列出两次(引入再次拼写错误的可能性)。

推荐的正确方法是什么?

variables perl strict

2
推荐指数
1
解决办法
58
查看次数

Local variable visibility in closures vs. local `sub`s

Perl 5.18.2 accepts "local subroutines", it seems.

Example:

sub outer()
{
    my $x = 'x';   # just to make a simple example

    sub inner($)
    {
        print "${x}$_[0]\n";
    }

    inner('foo');
}
Run Code Online (Sandbox Code Playgroud)

Without "local subroutines" I would have written:

#...
    my $inner = sub ($) {
        print "${x}$_[0]\n";
    }

    $inner->('foo');
#...
Run Code Online (Sandbox Code Playgroud)

And most importantly I would consider both to be equivalent.

However the first variant does not work as Perl complains:

Variable $x is not available at ...

where ... describes the …

perl closures subroutine

2
推荐指数
1
解决办法
182
查看次数