繁体   English   中英

perl从标量变量逐行读取

[英]perl read line-by-line from scalar variable

从抓取网站开始,我在标量变量$ res中有一个html文件。 我想逐行读取$ res中的html文件。 例如,while(我的$ line =)...

我需要将$ res打印到文本文件,然后读入文本文件吗?

您可以使用IO::Scalar模块。

man IO::Scalar

use 5.005;
use IO::Scalar;
$data = "My message:\n";

### Open a handle on a string, read it line-by-line, then close it:
$SH = new IO::Scalar \$data;
while (defined($_ = $SH->getline)) {
    print "Got line: $_";
}

while(<$SH>)也可以。

要解决此问题的Y部分 ,可以,您可以将标量变量视为输入源,并使用Perl的输入处理功能。 您只需open对变量的引用:

open my $fh, '<', \$res;
my $header = <$fh>;        # first "line" of $res
while (my $line = <$fh>) { # next "line" of $res
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM