Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl equiv to PHP file() ?

Reply
Thread Tools

Perl equiv to PHP file() ?

 
 
Amittai Aviram
Guest
Posts: n/a
 
      08-18-2003
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




 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-18-2003
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

 
Reply With Quote
 
 
 
 
Amittai Aviram
Guest
Posts: n/a
 
      08-18-2003

"Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
news:bhr4r8$29agi$(E-Mail Removed)-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



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-18-2003
Amittai Aviram <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-18-2003
Amittai Aviram wrote:
> "Gunnar Hjalmarsson" <(E-Mail Removed)> wrote in message
> news:bhr4r8$29agi$(E-Mail Removed)-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Training Institute In Delhi, Live Projects on PHP. Short TermPHP Courses, PHP Scripts, PHP Training with Live Projects. Rajive Narain Java 0 09-18-2009 10:47 AM
How to print stuff in RTHML embedded code (equiv to php "echo")?? Namor Ruby 7 03-07-2006 09:49 PM
JS equiv for PHP foreach() cordmcphail@gmail.com Javascript 8 02-14-2006 11:17 PM
java regex equiv to perl !~ kevinm3574 Java 7 08-03-2005 04:04 PM
Perl equiv to PHP print_r Aidan Perl Misc 2 03-18-2005 12:42 AM



Advertisments