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

Hi,

I want to open the file which is exists in subversion. It is hyperlink. I tried in the following way. my $fileName= "http://<domain>/test2.txt"; open (FILE,"< $fileName")||die("Error: Can't open file $fileName for reading"); my $file=""; while (<FILE>) { $file .= "$_"; } close FILE; I am getting the error like Can't open file $fileName for reading......... Please help me out on this. Thanks in advance, kavitha

  • Comment on How to open the file exists in web link

Replies are listed 'Best First'.
Re: How to open the file exists in web link
by CountZero (Bishop) on Feb 09, 2010 at 07:01 UTC
    Perl does not allow you to directly access a file over the HTTP-protocol through a filehandle.

    You should first download the file, save it somewhere on your system and then read the file from there. Or if the file is not too big, you can keep its contents in a variable (array or scalar) upon downloading.

    Checkout LWP or even LWP::Simple to learn how to download a file over HTTP.Or you can use IO::All which provides an object oriented filehandle-like interface.

    One more comment: you are using far too many double quotes in your small script. $file = $_; is all you need if you do not have to interpolate the variable.

    Update: changed typo + added reference to IO::All

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Perl does not allow you to directly access a file over the HTTP-protocol through a filehandle

      Perl has a pluggable IO system, so that's not really true. For example, it allows the following:

      open(my $fh, '<:lwp', 'http://...')

      The catch is the module providing the functionality might not exist yet.

        Indeed, I should have said "Without adding or writing modules, Perl ...".

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to open the file exists in web link
by ahmad (Hermit) on Feb 09, 2010 at 07:09 UTC

    You can use LWP::UserAgent for this

    For example:

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $URL = 'http://www.google.com'; my $UA = LWP::UserAgent->new(timeout => 10); my $response = $UA->get($URL); if ( $response->is_success ) { print $response->as_string; }else{ print "Couldn't open website: " . $response->status_line; }
Re: How to open the file exists in web link
by Anonymous Monk on Feb 09, 2010 at 07:01 UTC
    file aren't hyperlinks, so that won't work ... IO::All