Perl equiv to PHP file() ?

Discussion in 'Perl Misc' started by Amittai Aviram, Aug 18, 2003.

  1. 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
     
    Amittai Aviram, Aug 18, 2003
    #1
    1. Advertising

  2. 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/libwww-perl-5.69/lib/LWP/Simple.pm

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 18, 2003
    #2
    1. Advertising

  3. "Gunnar Hjalmarsson" <> wrote in message
    news:bhr4r8$29agi$-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/libwww-perl-5.69/lib/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
     
    Amittai Aviram, Aug 18, 2003
    #3
  4. Amittai Aviram <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 18, 2003
    #4
  5. Amittai Aviram wrote:
    > "Gunnar Hjalmarsson" <> wrote in message
    > news:bhr4r8$29agi$-berlin.de...
    >
    >> 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

    >
    > 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
     
    Gunnar Hjalmarsson, Aug 18, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevinm3574

    java regex equiv to perl !~

    kevinm3574, Aug 2, 2005, in forum: Java
    Replies:
    7
    Views:
    1,277
    kevinm3574
    Aug 3, 2005
  2. Rajive Narain
    Replies:
    0
    Views:
    1,885
    Rajive Narain
    Sep 18, 2009
  3. Namor
    Replies:
    7
    Views:
    116
    kwatch
    Mar 7, 2006
  4. Aidan

    Perl equiv to PHP print_r

    Aidan, Mar 17, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    687
    Aidan
    Mar 18, 2005
  5. JS equiv for PHP foreach()

    , Feb 14, 2006, in forum: Javascript
    Replies:
    8
    Views:
    150
    Thomas 'PointedEars' Lahn
    Feb 14, 2006
Loading...

Share This Page