Perl equiv to PHP file() ?

A

Amittai Aviram

Does Perl have an equivalent to the PHP function file()? file() reads a
file into an array. More importantly for me, you can pass a URL to file(),
and it will read into the array the HTML output of that file as served
through HTTP:

$output = file(''http://www.mysite.com/test.pl'')

This will read the HTML output of the test.pl executable and put each line
of it into a successive element of the $output array. (In PHP, $ designates
any variable, including an array -- it does not use @ or % to distinguish
variable types.)

It would be very helpful to me if I could find a Perl equivalent to PHP's
file(url). Thanks!

Amittai Aviram
 
G

Gunnar Hjalmarsson

Amittai said:
Does Perl have an equivalent to the PHP function file()? file()
reads a file into an array. More importantly for me, you can pass
a URL to file(), and it will read into the array the HTML output of
that file as served through HTTP:

$output = file(''http://www.mysite.com/test.pl'')

This will read the HTML output of the test.pl executable and put
each line of it into a successive element of the $output array.

use LWP::Simple;
@output = split /\n/, get('http://www.mysite.com/test.pl');

http://search.cpan.org/author/GAAS/libwww-perl-5.69/lib/LWP/Simple.pm
 
A

Amittai Aviram

Gunnar Hjalmarsson said:

GREAT! Thank you so much, Gunnar! One follow-up question ...

The getstore($url, $file) actually looks exactly like what I want, since it
gets the HTML document and "stores it in the file." But I'm not clear about
the code context. Should you first define $file as a file handle? Do you
need to open the file first before calling getstore? And then close it
afterwards? This may seem obvious to many here, but the documentation left
it unclear and perhaps the info would be helpful to other newbies, as well
as to me. Thanks again for the help.

Amittai
 
T

Tad McClellan

Amittai Aviram said:
you can pass a URL to file(),
and it will read into the array the HTML output of that file as served
through HTTP:


Your Question is Asked Frequently:

perldoc -q HTML

How do I fetch an HTML file?
 
G

Gunnar Hjalmarsson

Amittai said:
GREAT! Thank you so much, Gunnar! One follow-up question ...

The getstore($url, $file) actually looks exactly like what I want,
since it gets the HTML document and "stores it in the file."

Well, you said array...
But I'm not clear about the code context. Should you first define
$file as a file handle? Do you need to open the file first before
calling getstore? And then close it afterwards?

To be able to give you a reliable answer to that question, I did what
you should have done before asking: I tested. ;-)

$file is just the path to the file, and you don't need to open/close
it separately. This should do it:

use LWP::Simple;
getstore('http://www.mysite.com/test.pl', $file);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,356
Latest member
deepthi.kodakandla

Latest Threads

Top