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

Hi.

How can I open remote files (somewhere on the net) using the open() function?
Ie: open(FILE, 'http://www.yahoo.com/index.html');

I know the LWP module exists, but I would like to know if it's still possible to be done with the open() function.

Thanks,
Ralph.

Replies are listed 'Best First'.
Re: opening remote files
by Zaxo (Archbishop) on Sep 10, 2001 at 01:21 UTC

    It is possible if the remote file is on something like a nfs filesystem or a smb share. Out of the box, open() is restricted to the local filesystem.

    You could overload open() to use LWP, download to a temporary location and open the file there. If you do that, I think

    use URI; sub URI::open { use LWP; # fetch to /tmp and open # beware error conditions, # net adds a lot of new ones }
    would be a reasonable start. You might want to write a module subclassing URI instead.

    In my own code, I'd reserve open() for local operations.

    After Compline,
    Zaxo

Re: opening remote files
by toma (Vicar) on Sep 10, 2001 at 07:23 UTC
    If you install LWP, you get a very nice program called GET. This program gets the contents of a URL and prints it out. You can read from this output like a file by using a pipe in the open() command.
    use strict; open(FILE, 'GET http://www.yahoo.com/index.html |'); while(<FILE>) { print $_; } close FILE;

    If you don't want to use LWP there are other programs that can get the contents of a URL. These programs can be used the same way.

    It should work perfectly the first time! - toma

Re: opening remote files
by suaveant (Parson) on Sep 10, 2001 at 02:59 UTC
    Probably the easiest way to do it is copy the file to the local system with scp or rcp or Net::FTP or somehing like that, edit it, and then push it back up... all of with can be done within perl... you could overload or simply create an open routine which did that for you, and wrote it back on close...

                    - Ant
                    - Some of my best work - Fish Dinner

Re: opening remote files
by Anonymous Monk on Sep 10, 2001 at 14:21 UTC
    Ok, thanks a lot for all of your answers!

    Thanks,
    Ralph :)