Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Open web-adress as filehandle

by Lowry (Initiate)
on Aug 18, 2014 at 06:28 UTC ( #1097791=perlquestion: print w/replies, xml ) Need Help??

Lowry has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm seeking your wisdom. I'm looking for a way to open a file on the web without having to store it locally and use it similar to a filehandle.

E.g. something like (pseudo-code)
use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get($url); my $file_content = $mech->content() while(<$file_content>){ # do something }

As far as I know mechanize dies when the file isn't an html file. Additionally, I'm not sure if I can use the string returned in a filehandle way

Please enlighten me, someone.
Lowry

Replies are listed 'Best First'.
Re: Open web address as filehandle
by Athanasius (Archbishop) on Aug 18, 2014 at 06:44 UTC
      Awesome. That's exactly what I was looking for.

      Thanks a lot
      Lowry
      Athanasius, I wish I could upvote your post by more - one seems such an understatement for the idea that s/b and self-evidently wasn't to me, obvious.

      A user level that continues to overstate my experience :-))
Re: Open web-adress as filehandle
by Corion (Patriarch) on Aug 18, 2014 at 06:52 UTC

    Why would WWW::Mechanize die if the file doesn't have HTML content?

    Maybe you want to show an example that actually demonstrates this?

    For treating a scalar as a filehandle, see open on "in-memory filehandles".

      Sorry, you are right. It only dies if I try to get the content as text. Raw content shouldn't be a problem.
      And thanks for the "in-memory FH" hint. Will definitely look at it.

      Lowry

Re: Open web-adress as filehandle
by LanX (Saint) on Aug 18, 2014 at 08:19 UTC
    > open a file on the web .... mechanize ...

    As a side note, mechanize seems overkill for that task.

    While not strictly core, LWP::Simple should be on most systems as dependency of the cpan client (well IMHO I didn't check yet°)

    So just get("url") the $content and then open the reference \$content to a filehandle.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    °) I'm not sure anymore about the most basic "fetch from Web" mechanism of cpan-client, but LWP was one of the options my system used...

Re: Open web-adress as filehandle
by thomas895 (Deacon) on Aug 18, 2014 at 06:50 UTC

    $mech->content() returns a string.

    I know of only two ways to do this: an unlink'd temporary file and a FIFO(SysV IPC, but then to yourself).

    • An unlink'd temporary file. See File::Temp.
    • A FIFO. See IPC::SysV and Advanced Programming in the UNIX Environment by W. Richard Stevens, Chapter 14.5-14.9. (ISBN 0201563177)
    -Thomas
    "Excuse me for butting in, but I'm interrupt-driven..."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097791]
Approved by thomas895
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2023-09-28 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?