我如何告诉 Perl 函数从特殊的 ARGV 句柄中读取文件?

发布于 2024-09-29 20:42:31 字数 357 浏览 0 评论 0原文

perldoc perlvar 中,我读到了以下内容:

请注意,目前“ARGV”仅具有 其神奇功效在“<>”中 操作员;在其他地方它只是一个简单的 最后一个对应的文件句柄 由“<>”打开的文件。尤其, 将“*ARGV”作为参数传递给 需要文件句柄的函数可能 不会导致你的功能 自动读取全部内容 @ARGV 中的文件。

那么,我如何*ARGV (或类似的东西)作为参数传递给需要文件句柄的函数,并让该函数读取 < 中的所有文件代码>@ARGV?

In perldoc perlvar, I read this:

Note that currently "ARGV" only has
its magical effect within the "<>"
operator; elsewhere it is just a plain
filehandle corresponding to the last
file opened by "<>". In particular,
passing "*ARGV" as a parameter to a
function that expects a filehandle may
not cause your function to
automatically read the contents of all
the files in @ARGV.

So, how can I pass *ARGV (or something that resembles it) as a paramter to a function that expects a filehandle, and have that function read all the files in @ARGV?

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

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

发布评论

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

评论(3

无妨# 2024-10-06 20:42:31

继续并传入 *ARGV 类型 glob 或 \*ARGV 及其引用。只需确保最终使用它的函数是通过 <$fh> 运算符或 readline($fh)(其底层功能等效项)来实现的。

perlvar 联机帮助页中引用的段落中解决的问题只是想提醒您,如果您有任何操作,您将无法触发 ARGVmagic open在句柄上使用除readline之外的其他读取机制,例如readsysreadgetc

运行它来向自己证明它是有效的:

sub mycat {
    my $fh = shift;
    print "$ARGV $.: $_" while <$fh>;
}
mycat(*ARGV);

将其放入文件中,然后使用多个文件参数运行它:

% perl mycat ./mycat //`pwd`/mycat ~/mycat
./mycat 1: sub mycat {
./mycat 2:     my $fh = shift;
./mycat 3:     print "$ARGV $.: $_" while <$fh>;
./mycat 4: } 
./mycat 5: mycat(*ARGV);
///home/tchrist/mycat 6: sub mycat {
///home/tchrist/mycat 7:     my $fh = shift;
///home/tchrist/mycat 8:     print "$ARGV $.: $_" while <$fh>;
///home/tchrist/mycat 9: } 
///home/tchrist/mycat 10: mycat(*ARGV);
/home/tchrist/mycat 11: sub mycat {
/home/tchrist/mycat 12:     my $fh = shift;
/home/tchrist/mycat 13:     print "$ARGV $.: $_" while <$fh>;
/home/tchrist/mycat 14: } 
/home/tchrist/mycat 15: mycat(*ARGV);

看到了吗?效果很好。

Go right ahead and pass in the *ARGV typeglob or \*ARGV, its reference. Just make sure that the function eventually making use of it does so via the <$fh> operator or readline($fh), its underlying functional equivalent.

The issue addressed in the cited passage from the perlvar manpage is just trying to remind you that you won't be able to get ARGV’s magic open to trigger if you any use other reading mechanism than readline on the handle, such as read, sysread, or getc.

Run this to prove to yourself it works:

sub mycat {
    my $fh = shift;
    print "$ARGV $.: $_" while <$fh>;
}
mycat(*ARGV);

Put that in a file, then run it with several file arguments:

% perl mycat ./mycat //`pwd`/mycat ~/mycat
./mycat 1: sub mycat {
./mycat 2:     my $fh = shift;
./mycat 3:     print "$ARGV $.: $_" while <$fh>;
./mycat 4: } 
./mycat 5: mycat(*ARGV);
///home/tchrist/mycat 6: sub mycat {
///home/tchrist/mycat 7:     my $fh = shift;
///home/tchrist/mycat 8:     print "$ARGV $.: $_" while <$fh>;
///home/tchrist/mycat 9: } 
///home/tchrist/mycat 10: mycat(*ARGV);
/home/tchrist/mycat 11: sub mycat {
/home/tchrist/mycat 12:     my $fh = shift;
/home/tchrist/mycat 13:     print "$ARGV $.: $_" while <$fh>;
/home/tchrist/mycat 14: } 
/home/tchrist/mycat 15: mycat(*ARGV);

See? It works fine.

巷子口的你 2024-10-06 20:42:31

迭代重载对象时,您可以重新分派到 <>

{package ReadARGV;
    sub new {bless []}
    use overload '<>' => sub {<>};
}

sub reader {
    my $fh = shift;
    local $_;
    print while <$fh>;
}

reader(ReadARGV->new);

ReadARGV 包包含一个简单的对象构造函数 new,然后重载 > 运算符来简单地调用<>

You can redispatch to <> when iterating an overloaded object:

{package ReadARGV;
    sub new {bless []}
    use overload '<>' => sub {<>};
}

sub reader {
    my $fh = shift;
    local $_;
    print while <$fh>;
}

reader(ReadARGV->new);

The ReadARGV package contains a simple object constructor new and then overloads the <HANDLE> operator to simply call <>.

温柔嚣张 2024-10-06 20:42:31

你可以用cat来伪造它(在Unix系统上),

open FAKE_ARGV, "cat @ARGV |";
function_expecting_filehandle(*FAKE_ARGV);
...

这样你就失去了一些魔力($ARGV$.)。

我确信也有一种方法可以处理绑定的文件句柄(请参阅 perltie )。

You could fake it with cat (on a Unix system)

open FAKE_ARGV, "cat @ARGV |";
function_expecting_filehandle(*FAKE_ARGV);
...

You lose some of the magic ($ARGV, $.) with this.

I'm sure there's a way to do with a tied filehandle, too (see perltie).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文