perl:path + dirs通过引用传递给sub的数组将无法正确打印或打开

时间:2018-03-16 14:40:11

标签: perl

我需要打开并使用文件的内容,因为它们作为参考传递到我的子例程中。我已经研究并找到了

How to pass a file handle to a function?

但这对我不起作用。

我已经大大减少了代码,以达到目的。在这里。

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

my @dirs = qw(/opt/r2configs/sapbid00/2018/03/15/lparstat-i.out /opt/r2configs/sapbiq00/2018/03/15/lparstat-i.out);

sub _ce {

    die "no parameter!\n" unless @_;
    my ($ce_ref) = @_;

    for my $name (@{ $ce_ref }) {
        print "$name\n";
        chomp $name;
        open (my $i, "+<", $name) or warn $!;
        while (<$i>) {
            print "$i,\n";
        }
    }
}

_ce( \@dirs );
# perl -c foo
foo syntax OK
root@r2nim01.xxx.com(/usr/local/scripts)$
# perl foo
/opt/r2configs/sapbid00/2018/03/15/lparstat-i.out
GLOB(0x20020148),
GLOB(0x20020148),
GLOB(0x20020148),
GLOB(0x20020148),
GLOB(0x20020148),
/opt/r2configs/sapbiq00/2018/03/15/lparstat-i.out
GLOB(0x20020148),
GLOB(0x20020148),
.
.
.

### The files do exist and I am root user. ###
# ls -l /opt/r2configs/sapbiq00/2018/03/15/lparstat-i.out
-rw-r--r--    1 root     system         2217 Mar 15 20:35 /opt/r2configs/sapbiq00/2018/03/15/lparstat-i.out

2 个答案:

答案 0 :(得分:2)

    while (<$i>) {
        print "$i,\n";
    }

在此处打印文件中每行的文件句柄。打印您阅读的行:

print "$_,\n";

更好的是:

while (my $line = <$i>) {
    print "$line,\n";
}

答案 1 :(得分:2)

您正在输出文件句柄,而不是内容。

const mapStateToProps = (state) => ({
  apolloLink: state.apollo.apolloLink,
});
export default connect(mapStateToProps)(Dashboard);

while (<$i>) { print "$i,\n"; # <--- here } 是句柄。由于您在没有变量的情况下阅读,因此需要查看$i

$_

您可能还希望根据惯例命名句柄print "$_,\n"; # <--- here $fh通常用于计算某些内容的$i循环迭代变量。