Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Perl equiv to PHP file() ? (http://www.velocityreviews.com/forums/t881914-perl-equiv-to-php-file.html)

Amittai Aviram 08-18-2003 05:05 PM

Perl equiv to PHP file() ?
 
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





Gunnar Hjalmarsson 08-18-2003 06:10 PM

Re: Perl equiv to PHP file() ?
 
Amittai Aviram wrote:
> 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/l.../LWP/Simple.pm

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


Amittai Aviram 08-18-2003 06:32 PM

Re: Perl equiv to PHP file() ?
 

"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:bhr4r8$29agi$1@ID-184292.news.uni-berlin.de...
> Amittai Aviram wrote:
> > 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/l.../LWP/Simple.pm
>


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




Tad McClellan 08-18-2003 06:47 PM

Re: Perl equiv to PHP file() ?
 
Amittai Aviram <amittai@amittai.com> wrote:

> 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?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Gunnar Hjalmarsson 08-18-2003 06:48 PM

Re: Perl equiv to PHP file() ?
 
Amittai Aviram wrote:
> "Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
> news:bhr4r8$29agi$1@ID-184292.news.uni-berlin.de...
>
>> use LWP::Simple;
>> @output = split /\n/, get('http://www.mysite.com/test.pl');
>>
>> http://search.cpan.org/author/GAAS/l.../LWP/Simple.pm

>
> 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);

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



All times are GMT. The time now is 05:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57