Why doesn't this work?

S

Sherm Pendley

David said:
#!/usr/bin/perl

use File::Copy;

$tempfile = "http://www.domain.com/temp.txt";
copy($tempfile,"temp2.txt");


How would you call a file from another server?

Perl's built-in copy() function only works for local files. To read a file
from a URL, see the LWP or WWW::Mechanize modules on CPAN.

sherm--
 
J

Jürgen Exner

David said:
#!/usr/bin/perl

use File::Copy;

$tempfile = "http://www.domain.com/temp.txt";
copy($tempfile,"temp2.txt");

It doesn't work because copy() copies files and
http://www.domain.com/temp.txt is not a file but a URL (which may or may not
resolve into a file on the server side but the client wouldn't know anything
about that).
How would you call a file from another server?

_Another_ server? Well, whatever.
To download the reply to an http request you would use e.g. the LWP module.

jue
 
D

David Grey

Jürgen Exner said:
It doesn't work because copy() copies files and
http://www.domain.com/temp.txt is not a file but a URL (which may or may not
resolve into a file on the server side but the client wouldn't know anything
about that).


_Another_ server? Well, whatever.
To download the reply to an http request you would use e.g. the LWP module.

jue


Thanks, I found this after going to a bunch of pages in google that
were asking how to use it and finally came across the form:

# Create a user agent object
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");

# Create a request
my $req = HTTP::Request->new(POST => 'http://www.perl.com/cgi-bin/BugGlimpse');
$req->content_type('application/x-www-form-urlencoded');
$req->content('match=www&errors=0');

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

http://www.perldoc.com/perl5.8.0/lib/LWP.html

I don't think I would want to POST I think I would want to GET,
and then it did not have any copy examples. Can someone give
me a hint on this as well? I assume the use File::Copy; will not
work as pointed out.

Thanks.
 
S

Sherm Pendley

David said:
Thanks, I found this after going to a bunch of pages in google that
were asking how to use it

Google is nice, but did you try looking on your own computer?

perldoc LWP
perldoc LWP::Simple
perldoc lwpcook

sherm--
 
T

Tad McClellan

David Grey said:
Thanks, I found this after going to a bunch of pages in google that
were asking how to use it and finally came across the form:

# Create a user agent object
use LWP::UserAgent;

I don't think I would want to POST I think I would want to GET,


use LWP::Simple;
my $txt_file = get 'http://www.domain.com/temp.txt';

and then it did not have any copy examples. Can someone give
me a hint on this as well?


perldoc LWP::Simple
 
D

David Wheat

Sherm said:
Google is nice, but did you try looking on your own computer?

perldoc LWP
perldoc LWP::Simple
perldoc lwpcook

I have no way to access that. It seems like the LWP is not installed
on my host. Unless these will cause a 500 error.

#!/usr/bin/perl

LWP::UserAgent;

$ua = LWP::UserAgent->new;

$ua->proxy(['http', 'ftp'], 'http://www.domain.com/');
$req = new HTTP::Request('GET' => 'http://www.domain.com/temp.txt');

--
or
--

#!/usr/bin/perl

use LWP::Simple;
$doc = get 'http://www.domain.com/temp.txt';
 
J

John Bokma

Purl said:
David Wheat wrote:




Yes, LWP is never installed on webservers. Try

Funny:

jbokma:~$ perl -e 'use LWP;print "This is libwww-perl-$LWP::VERSION\n"'
This is libwww-perl-5.76

And I didn't even ask for it to be installed :-D.
 
C

Chris Mattern

David said:
#!/usr/bin/perl

# always, always, ALWAYS include these in your code:
use warnings;
use strict;
use File::Copy;

$tempfile = "http://www.domain.com/temp.txt";
copy($tempfile,"temp2.txt");
Well, no. File::Copy copies one local file to another. It's
not going to connect to other servers for you.
How would you call a file from another server?

Depends on how I need to call it. For HTTP, I'd use
LWP::Simple, which should have come with your perl install.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
B

Bob Walton

David Grey wrote:

....
....



I don't think I would want to POST I think I would want to GET,
and then it did not have any copy examples. Can someone give
me a hint on this as well? I assume the use File::Copy; will not
work as pointed out.


[code below is all one line, change shell quoting if not on Windoze]:

perl -MLWP::Simple -e "getprint 'http://bwalton.com/'">temp2.txt
 
J

John Bokma

Your server enjoys a wise and well prepared administrator.
You are most fortunate.

As always. Also my home XP box has LWP installed, default. And sometimes
a webserver (Apache) :-D.

Never say never ;-)
 
J

Jürgen Exner

David said:
I have no way to access that.

And why not? As far as I am aware LWP is a standard module in all recent
Perl versions.
Is your Perl installation broken? Then you may want to re-install or kick
you admin until he fixes the broken installation.
It seems like the LWP is not installed on my host.

What does the software that is installed or not installed on your host have
to do with the documentation that you can access on your own computer?

jue
 
B

Bob Walton

David Grey wrote:

....
....



I don't think I would want to POST I think I would want to GET,
and then it did not have any copy examples. Can someone give
me a hint on this as well? I assume the use File::Copy; will not
work as pointed out.


[code below is all one line, change shell quoting if not on Windoze]:

perl -MLWP::Simple -e "getprint 'http://bwalton.com/'">temp2.txt
 
D

Dave Cross

LWP is never installed on webservers.

Actually, LWP has been included in the standard Perl distribution since
July 2002 so any web server that has a 5.8.x version of Perl will
automatically have LWP installed.

Dave...
 
C

Chris Mattern

Purl said:
Did you know the word "gullible" is not found
in any dictionary?
Look, if you're going try to distract us from how wrong you are--again--
why not go for a more classic style? Point out the Winged Victory of
Samothrace, or something.
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
R

Richard Morse

David Wheat <[email protected]> said:
I have no way to access that. It seems like the LWP is not installed
on my host. Unless these will cause a 500 error.

#!/usr/bin/perl

LWP::UserAgent;

$ua = LWP::UserAgent->new;

$ua->proxy(['http', 'ftp'], 'http://www.domain.com/');
$req = new HTTP::Request('GET' => 'http://www.domain.com/temp.txt');

--
or
--

#!/usr/bin/perl

use LWP::Simple;
$doc = get 'http://www.domain.com/temp.txt';

Neither of these scripts is going to do what it seems like you want. A
'500 error', if I'm guessing correctly, is due to you not returning
proper headers from a CGI call.

First, try to make in run on your _local_ computer (ie, not as a CGI
script). For instance:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;

my $doc = get 'http://www.domain.com/temp.txt';
print $doc;

__END__

Then you can deal with making it a CGI:

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use CGI;

my $doc = get 'http://www.domain.com/temp.txt';
my $q = new CGI;
print $q->header('text/plain');
print $doc;

__END__

(And yes, Ms. Gurl, I am aware that this is exceedingly inefficient. It
his, however, easy to understand, which it seems like the OP might need.)

Once you've done this, you can now sit down and determine how these
differ from your above examples.

As an exercise, figure out how to make the LWP::UserAgent version work.

HTH,
Ricky
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,841
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top