Perl: Read web text file and “open” it

自闭症网瘾萝莉.ら 提交于 2019-12-22 06:44:02

问题


I'm trying to create a script that will read text files and then analyse them, regardless of whether the text file is online or offline.

The offline part is done, using

open(FILENAME, "anyfilename.txt")
analyze_file();

sub analyze_file {
   while (<FILENAME>) {analyze analyze}
}

Now for the online part, is there anyway to read a text file on a website and then "open" it?

What I hope to achieve is this:

if ($offline) {
   open(FILENAME, "anyfilename.txt")
}
elsif ($online) {
   ##somehow open the http web text so that I can do a while (<FILENAME>) later
}

analyze_file();

sub analyze_file {
   while (<FILENAME>) {analyze analyze}
}

There's the "get('http://weblink.com/textfile.txt;)" but it creates a string. I can't do a while () with that string.

Does anyone know how this can be done?


回答1:


It's simple, just use the open FILEHANDLE,MODE,REFERENCE style of open.

use LWP::Simple;
if ($offline) {
   open( FILENAME, '<', "anyfilename.txt" )
}
elsif ($online) {
   my $text = get 'http://example.com';
   open( FILENAME, '<', \$text );
}


来源:https://stackoverflow.com/questions/8618524/perl-read-web-text-file-and-open-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!