如何从命令行获取文件句柄?
我有一个以文件句柄作为参数的子例程。 如何从命令行指定的文件路径创建文件句柄? 我不想自己对这个文件进行任何处理,我只想将其传递给另一个子例程,该子例程返回一个包含文件中所有解析数据的哈希数组。
我使用的命令行输入如下所示:
$ ./getfile.pl /path/to/some/file.csv
这是我调用的子例程的开头如下所示:
sub parse {
my $handle = shift;
my @data = <$handle>;
while (my $line = shift(@data)) {
# do stuff
}
}
I have a subroutine that takes a filehandle as an argument. How do I make a filehandle from a file path specified on the command line? I don't want to do any processing of this file myself, I just want to pass it off to this other subroutine, which returns an array of hashes with all the parsed data from the file.
Here's what the command line input I'm using looks like:
$ ./getfile.pl /path/to/some/file.csv
Here's what the beginning of the subroutine I'm calling looks like:
sub parse {
my $handle = shift;
my @data = <$handle>;
while (my $line = shift(@data)) {
# do stuff
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
命令行参数在预定义的@ARGV 数组中可用。 您可以从那里获取文件名并使用
open
打开它的文件句柄。 假设您想要对该文件进行只读访问,您可以这样做:请注意,
or die...
检查调用open
是否成功,并以以下方式结束:如果不是,则显示错误消息。 内置变量$!
将包含失败时的(取决于操作系统)错误消息,告诉您调用不成功的原因。 例如“权限被拒绝”。Command line arguments are available in the predefined
@ARGV
array. You can get the file name from there and useopen
to open a filehandle to it. Assuming that you want read-only access to the file, you would do it this way:Note that the
or die...
checks the callopen
for success and dies with an error message if it wasn't. The built-in variable$!
will contain the (OS dependent) error message on failure that tells you why the call wasn't successful. e.g. "Permission denied."parse(*ARGV)
是最简单的解决方案:解释有点长,但是学习如何有效使用 Perl 的一个重要部分就是学习 Perl。当您使用空文件句柄 (
<>
) 时,它实际上从神奇的ARGV
文件句柄中读取,该文件句柄具有特殊的语义:它从<> 中命名的所有文件读取。 code>@ARGV
,如果@ARGV
为空,则为STDIN
。来自perldoc perlop:
您不必在
while
循环中使用<>
--my $data = <>
将从第一个非空文件中读取一行,my @data = <>;
会将其全部吞掉立即,您可以传递*ARGV
,就好像它是一个普通的文件句柄一样。parse(*ARGV)
is the simplest solution: the explanation is a bit long, but an important part of learning how to use Perl effectively is to learn Perl.When you use a null filehandle (
<>
), it actually reads from the magicalARGV
filehandle, which has special semantics: it reads from all the files named in@ARGV
, orSTDIN
if@ARGV
is empty.From
perldoc perlop
:You don't have to use
<>
in awhile
loop --my $data = <>
will read one line from the first non-empty file,my @data = <>;
will slurp it all up at once, and you can pass*ARGV
around as if it were a normal filehandle.这就是 -n 开关的用途!
采用您的解析方法,并执行以下操作:
每一行都存储在 $_ 中。 所以你运行
./getfile.pl /path/to.csv
它会这样做。
请参阅此处 和此处了解有关这些的更多信息。 我也喜欢 -p,并且发现 -a 和 -F 的组合非常有用。
另外,如果您想进行一些额外的处理,请添加 BEGIN 和 end 块。
管他呢。 这非常非常有用。
This is what the -n switch is for!
Take your parse method, and do this:
Each line is stored in $_. So you run
./getfile.pl /path/to.csv
And it does this.
See here and here for some more info about these. I like -p too, and have found the combo of -a and -F to be really useful.
Also, if you want to do some extra processing, add BEGIN and end blocks.
or whatever. This is very, very useful.
我是否遗漏了某些内容,或者您只是在寻找 open() 调用?
Am I missing something or are you just looking for the open() call?