Never coded in PERL before. I need to create a very small PERL script that simply opens a URL and then prints it. Actually I need to do a bit more than that but haven't got past this first base yet.
I found a few tutorials around on the web for PERL and ended up with this:-
#!/usr/bin/perlprint "testing\n";
$FilePath = "https://localhost/";
sysopen (FH, $FilePath, r);
print <FH>;
close(FH);
But it doesn't do what I intended it to do which is:-
1. Print to screen "testing" <- that bit works at least ;)
2. Open a URL residing on my local Apache-SSL service (in this case just the root index.html)
3. Print the contents of that page
4. Release the file handle.
I run it from the command line as:-
perl perltest.pl
It prints the "testing" OK but nothing else. No errors.
Can anyone help? Also, the perldoc website (where I found various functions) doesn't tell me what return type to expect from sysopen. What's the best way to examine the return for errors?
I'm guessing I may need some kind of "get" function, but a search for wget on the perldoc website doesn't reveal anything. Nor did a search for "get" reveal anything which looked right.
I don't really want to go through a full PERL tutorial as this is the first, and possibly last, time I am likely to need to use it and it'll be a 7 or 8 line script, tops.
Thanks!
#!/usr/bin/perluse LWP::Simple;
my $url = 'http://www.google.com/';
my $content = get $url;
die "Couldn't get $url" unless defined $content;
print $content;
Is the "unless defined" conditional here appropriate?
Thanks.
use LWP::UserAgent [search.cpan.org];
use HTTP::Request [kobesearch.cpan.org];
my $URL = 'http://www.example.com/';
my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request);
# Check the outcome of the response
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
print "\nContent:\n";
print $response->as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}
[edited by: phranque at 2:50 pm (utc) on Jan. 7, 2009]
I'm starting to like PERL. Might have to experiment a little more with it.
Incidentially, how can I get at the $response->main_content_of_page ?
Other than error checking the headers, I don't really need them.
HTTP::Message
[search.cpan.org...]
HTTP::Response
[search.cpan.org...]
you probably want to use some existing CPAN modules:#!/usr/local/bin/perluse LWP::UserAgent;
use HTTP::Request;my $URL = 'http://www.example.com/';
my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request);# Check the outcome of the response
if ($response->is_success){
print "URL:$URL\nHeaders:\n";
print $response->headers_as_string;
print "\nContent:\n";
print $response->as_string;
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML;
}
What I want to know is how I go about using a cpan module. I suppose I would need to download something and install it on my server before I can USE LWP and USE HTTP?
If so, what is the best procedure for doing that? I run Apache/2.0.54 (Fedora) Server, which I bought maybe at most 3 years ago and it has whatever version of Perl was au fait at the time. It's on a nice juicy dell poweredge machine.
Maybe just reading the blue camel book from start to finish is going to make the biggest difference to my skill level, but I am keen to become properly proficient in using cpan, modules and packages properly, and indeed becoming the best perl programmer I can be, with as much ability in the field of OOP as is necessary. (Which is what?, by the way)
Hope you can help. I think I'm off to have some tea and cake and sit down with the blue camel book. I'll just go take it off my shelf right now. Terrible place for a book, a shelf. Needs to be open and in front of you - at least sometimes!
cheers
Shams
More reading for you mate:
[cpan.org...]
The Camel book also has a CPAN chapter, at least mine does (Third Edition)
What I want to know is how I go about using a cpan module. I suppose I would need to download something and install it on my server before I can USE LWP and USE HTTP?
yes. while many modules are probably already installed on your system, others may not. Those can be compiled (in case of c/xs-stuff) and installed
a) manually (and who'd want that)
b) using something like CPAN.pm (a perl module that does the installing for you ... run it with "perl -MCPAN -e shell" and just hack away -- might want to do that in a virtual machine or non-production environment in case anything breaks) and
c) using your favorite package manager. I'm a debian-guy myself, but I'm sure that fedora has many pre-packaged perl-modules as well. Use yum (that was the package manager on fedora, wasn't it?) and just do a search for, say, lwp. you should find a few perl-modules and you could just have yum download and install them.
If I do that on debian, I get a list like
jan@lsrv01:~$ apt-cache search lwp
libcrypt-ssleay-perl - Support for https protocol in LWP
libhttp-access2-ruby1.8 - HTTP accessing library for ruby
libhttp-cache-transparent-perl - Perl module used to transparently cache HTTP requests
libhttp-proxy-perl - A pure Perl HTTP proxy
liblwp-authen-wsse-perl - Library for enabling X-WSSE authentication in LWP
liblwp-dev - Light Weight Processes library (development)
liblwp-protocol-http-socketunix-perl - Perl module to speak http through unix sockets
liblwp2 - Light Weight Processes library
libwhisker-perl - Perl module geared for HTTP testing
libwww-freshmeat-perl - automates searches on Freshmeat.net
libwww-indexparser-perl - Fetch and parse the directory index from a web server
libwww-perl - WWW client/server library for Perl (aka LWP)
Say I wanted to install LWP::UserAgent, so I'd run
apt-get install libwww-perl
and have apt-get set it up for me. It's as easy as that, but there are many much better introductions. I should also add that there's an improved version of CPAN.pm, called CPAN Plus, but I don't use that yet.