如何传递和读取Perl子例程的文件句柄?(How to pass and read a File handle to a Perl subroutine?)

编程入门 行业动态 更新时间:2024-10-27 11:25:44
如何传递和读取Perl子例程的文件句柄?(How to pass and read a File handle to a Perl subroutine?)

我想要一个从特殊文件句柄<STDIN>中读取的Perl模块,并将其传递给子例程。 你会明白我的意思,当你看到我的代码。 以前是这样的:

#!/usr/bin/perl use strict; use warnings; use lib '/usr/local/custom_pm' package Read_FH sub read_file { my ($filein) = @_; open FILEIN, $filein or die "could not open $filein for read\n"; # reads each line of the file text one by one while(<FILEIN>){ # do something } close FILEIN;

此时子例程将文件名(存储在$filein )作为参数,使用文件句柄打开文件,然后使用罚款句柄逐个读取文件的每一行。

相反,我想从<STDIN>获取文件名,将其存储在变量中,然后将该变量作为参数传递给子例程。 从主程序:

$file = <STDIN>; $variable = read_file($file);

该模块的子程序如下所示:

#!/usr/bin/perl use strict; use warnings; use lib '/usr/local/custom_pm' package Read_FH # subroutine that parses the file sub read_file { my ($file)= @_; # !!! Should I open $file here with a file handle? !!!! # read each line of the file while($file){ # do something }

有谁知道我该怎么做? 我很欣赏任何建议。

I want a Perl module that reads from the special file handle, <STDIN>, and passes this to a subroutine. You will understand what I mean when you see my code. Here is how it was before:

#!/usr/bin/perl use strict; use warnings; use lib '/usr/local/custom_pm' package Read_FH sub read_file { my ($filein) = @_; open FILEIN, $filein or die "could not open $filein for read\n"; # reads each line of the file text one by one while(<FILEIN>){ # do something } close FILEIN;

Right now the subroutine takes a file name (stored in $filein) as an argument, opens the file with a file handle, and reads each line of the file one by one using the fine handle.

Instead, I want get the file name from <STDIN>, store it inside a variable, then pass this variable into a subroutine as an argument. From the main program:

$file = <STDIN>; $variable = read_file($file);

The subroutine for the module is below:

#!/usr/bin/perl use strict; use warnings; use lib '/usr/local/custom_pm' package Read_FH # subroutine that parses the file sub read_file { my ($file)= @_; # !!! Should I open $file here with a file handle? !!!! # read each line of the file while($file){ # do something }

Does anyone know how I can do this? I appreciate any suggestions.

最满意答案

一般来说,使用词法文件处理程序是一个好主意。 这是一个包含文件处理程序而不是空白字的词法变量。

你可以像任何其他变量一样传递它。 如果使用File :: read_file ,则不需要单独的文件处理程序,它会将内容写入变量。

由于最好尽快关闭打开的文件句柄,如果您只需要获取完整的文件内容,这应该是首选方式。

使用File :: Slurp:

use strict; use warnings; use autodie; use File::Slurp; sub my_slurp { my ($fname) = @_; my $content = read_file($fname); print $content; # or do something else with $content return 1; } my $filename = <STDIN>; my_slurp($filename); exit 0;

没有额外的模块:

use strict; use warnings; use autodie; sub my_handle { my ($handle) = @_; my $content = ''; ## slurp mode { local $/; $content = <$handle> } ## or line wise #while (my $line = <$handle>){ # $content .= $line; #} print $content; # or do something else with $content return 1; } my $filename = <STDIN>; open my $fh, '<', $filename; my_handle($fh); # pass the handle around close $fh; exit 0;

It is a good idea in general to use lexical filehandlers. That is a lexical variable containing the file handler instead of a bareword.

You can pass it around like any other variables. If you use read_file from File::Slurp you do not need a seperate file handler, it slurps the content into a variable.

As it is also good practice to close opened file handles as soon as possible this should be the preferred way if you realy only need to get the complete file content.

With File::Slurp:

use strict; use warnings; use autodie; use File::Slurp; sub my_slurp { my ($fname) = @_; my $content = read_file($fname); print $content; # or do something else with $content return 1; } my $filename = <STDIN>; my_slurp($filename); exit 0;

Without extra modules:

use strict; use warnings; use autodie; sub my_handle { my ($handle) = @_; my $content = ''; ## slurp mode { local $/; $content = <$handle> } ## or line wise #while (my $line = <$handle>){ # $content .= $line; #} print $content; # or do something else with $content return 1; } my $filename = <STDIN>; open my $fh, '<', $filename; my_handle($fh); # pass the handle around close $fh; exit 0;

更多推荐

本文发布于:2023-07-26 05:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271377.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:句柄   例程   文件   Perl   pass

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!