Perl: read the text file and open it

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

The autonomous part is executed using

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

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

Now for the online part, does it matter to read a text file on a website and then “open” it?

I hope to achieve 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 is "get (" http://weblink.com/textfile.txt;) ", but it creates a string. I cannot do while () with this string.

Does anyone know how to do this?

+5
source share
1 answer

It's simple, just use the open FILEHANDLE,MODE,REFERENCEstyle open.

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

All Articles