简体   繁体   English

Getstore到Buffer,不使用临时文件

[英]Getstore to Buffer, not using temporary files

I've started Perl recently and mixed quite a bit of things to get what I want. 我最近刚开始Perl,并且混合了很多东西来获得我想要的东西。

My script gets the content of a webpage, writes it to a file. 我的脚本获取网页的内容,并将其写入文件。

Then I open a filehandler, plug the file report.html in (sorry i'm not english, i don't know how to say it better) and parse it. 然后,我打开一个文件处理程序,将文件report.html插入(很抱歉,我不是英语,我不知道怎么说的意思)并进行解析。 I write every line i encounter to a new file, except lines containing a specific color. 我将遇到的每一行都写入一个新文件,但包含特定颜色的行除外。

It works, but I'd like to try another way which doesn't require me to create a "report.html" temporary file. 它可以工作,但是我想尝试另一种不需要我创建“ report.html”临时文件的方法。

Furthermore, I'd like to print my result directly in a file, I don't want to have to use a system redirection '>'. 此外,我想直接将结果打印在文件中,我不想使用系统重定向'>'。 That'd mean my script has to be called by another .sh script, and I don't want that. 那意味着我的脚本必须由另一个.sh脚本调用,而我不希望那样。

use strict;
use warnings;
use LWP::Simple;

my $report = "report.html";

getstore('http://test/report.php', 'report.html') or d\
ie 'Unable to get page\n';

open my $fh2, "<$report" or die("could not open report file : $!\n");

while (<$fh2>) 
{
 print if (!(/<td style="background-color:#71B53A;"/ .. //));
}

close($fh2);

Thanks for your help 谢谢你的帮助

If you have got the html content into a variable, you can use a open call on this variable. 如果您将html内容放入变量中,则可以对此变量使用open调用。 Like: 喜欢:

my $var = "your html content\ncomes here\nstored into this variable";
open my $fh, '<', \$var;

# .. just do the things you like to $fh

You can try get function in LWP::Simple Module ;) 您可以尝试在LWP::Simple Module中get功能;)

To your sencond question, use open like open $fh, '<', $filepath . 对于第二个问题,请使用open像open $fh, '<', $filepath you can use perldoc -f open to see more info. 您可以使用perldoc -f open查看更多信息。

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

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