我的脚本使用LWP :: Simple的get()函数从互联网下载了纯文本文件。
我想以文件句柄的方式处理此字符串。我从http://www.perlmonks.org/?node_id=745018找到了这种“优雅”的方式(我喜欢)。
my $filelike = get($url); # whole text file sucked up in single string
open my $fh, '<', \$filelike or die $!;
while (<$fh>) {
# do wildly exciting stuff;
};
但是我喜欢使用FileHandle; 但是,我还没有找到使用它的方法。所以:
my $filelike = get($url);
my $fh = new FileHandle \$filelike; # does not work
my $fh = new FileHandle $filelike; # does not work either
有任何想法吗?
谢谢。
FileHandle提供了fdopen
一种可以从符号引用中为您提供FileHandle对象的方法。您可以打开标量引用的原始文件句柄,然后将其包装在FileHandle对象中。
open my $string_fh, '<', \$filelike;
my $fh = FileHandle->new->fdopen( $string_fh, 'r' );
(另外,请参见此答案以了解为什么应使用它Class->new
而不是间接new Class
表示法。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句