PDA

View Full Version : Need help writing a Perl script



elnino
08-01-2002, 01:49 AM
Hi, I need help writing a perl script that will be run as a cron job. I need the script to connect to a webserver and download a specific page (just the source, no pictures or anything), and then save that to my server. I tried a script already (attached) but it says unknown error on line 25 when I tried using it (in Windows with ActivePerl). If anyone could help me out, that would be great. Thanks!

mr_ego
08-01-2002, 10:31 AM
obviously (hopefully) you are running your own webserver.

all you have to do is go to CONTROL PANEL > SCHEDUALED TASKS (im 14 so dont ask me how to spell schedualed). and then add a schedualed task to be executed at the time you want. for exacmple, you want your script (probabily url.pl) to be executed every hour on the hour.

Benahimvp
08-01-2002, 10:40 AM
What does being 14 have to do with knowing how to spell? Just curious...

mr_ego
08-01-2002, 11:17 AM
who knows ... i had to find some excuse for misselling it.

elnino
08-01-2002, 12:10 PM
My perl script that I attached doesn't work though, I need help writing a new one or somehow fix the one I have. To me there doesn't seem like there is anything wrong with it. The script looks like it'll print the source of the website to the screen, and it shouldn't be hard to have it write to a file instead. It'll be running as a cron job on a Novell server that runs Apache as the webserver.

Mouldy_Goat
08-06-2002, 11:30 AM
Don't bother with using the low-level Socket module - it's more hassle than it's worth, try taking a look at this (http://www.perldoc.com/perl5.8.0/pod/perlipc.html#A-Webget-Client) example webget client, it's what you're after I think.
You'll have to hack about with it a bit of course, but I always use this as a base when I'm playing with sockets, for some reason I can never remember how to use them off the top of my head...

Mouldy_Goat
08-06-2002, 11:43 AM
Ok, after actually looking at your code I thought I'd just post a couple of points...
1)# #!/usr/local/bin/perl -w
Why have you commented out the shebang line....?
Just leave it as #!/usr/local/bin/perl -w and even if it's on a windows system where the path isn't important it'll give you warnings like you want.

2)die "Usage $0 host page \n" if $#ARGV <1;
Ok, this looks exactly like you've tried to hack around with a webget client... however you don't want to be doing things like this in a CGI context. Arguments aren't passed to CGI scripts in the same way that they are on the command line. This script will always die with this line in because the script will (probably) never have any arguments passed to it through the @ARGV array.

If you want this to work as a CGI script then you'd have to do something like this:


#!/usr/local/bin/perl -w
use CGI qw(:standard);
$host = param('host');
$page = param('page');

print header, start_html("Error"), h1("Usage $ENV{'SCRIPT_NAME'} host page"), end_html() if (!($host && $page));

print header;
print GetHTTP($host, $page);

3)
do {
$line = <SOCK>
} until ($line =~ /^\r\n/);
@output = <SOCK>;
close (SOCK) || die "close: $!";
@output;

This whole bit is a weee bit dodgy. The $line = <SOCK> bit hasn't got a semicolon on the end, the $line variable is being overwritten all the time and is therefore useless, the loop end condition is not certain so there's a possibility of an infinite loop (this is bad), @output will not be assigned any value, and I have a feeling that just writing @output; on a line on its own may raise warnings (I could be wrong though).

You could just do:

while ( <SOCK> ) { print }
instead.

Hope that helps a bit...