in reply to Reaped: Testing the network connection
seems:foreach(@sites){ $site=$_; # call the sub &getpages($site); }
is a little smoother. You're doing some mixing of your subs and globals: if you open OUTFILE, print @data to it, you should close it. If @data is a global, it probably shouldn't be populated in a sub. It can be but it's not a great coding practice. Subs should work on local, er, "my" data structures and return anything that the main routine needs to use. As in, you're getpages is ref/using the global $site, not the param, so it'd be better as:foreach my $site ( @sites) { &getpages($site); }
and notice I took the comment off of your print (print STDERR might be better). That's why you have $debug, though *I* prefer using debug to set various levels so you can have it on for just the code locations you need as in:sub getpages{ # start timer my $t0 = [gettimeofday]; # get the page my $site = shift; get("$site"); # stop timer my $t1 = [gettimeofday]; # write the values my $elapsed = tv_interval $t0, $t1; &writevalues($elapsed); if($debug){ print "Site $site took $elapsed seconds to load\n"; } } # sub getpages
Your writevalues should then "my $elapsed = shift;" - yes, seems a bit cumbersome, but it'll help make this a bit more readable to the next monk who's reading your code (of course, you'd add "return $elapsed;" to sub getpages). Hmm, writevalues also seems troubled by the same thing, I guess I'd have put getpages and writevalues in the foreach $site loop in the main rather than have getpages call writevalues. I'd initialize @data w/ the date/time (I think:$data[$[] = scalar localtime; would be one way to get what you want) *before* you started your foreach loop, skip the $space var and put it in your output loop, which'd be:if ( $debug > 10 ) { print STDERR "Site $site took $elapased seconds to load\n"; }
As for the next part (whoof, I'm tired), well you should probably be sure $samples isn't zero, er.$data[$[] = scalar localtime; foreach my $site (@sites){ # call the sub my $elapsed_time = &getpages($site); push @data, writevalues($elapsed_time); } open(OUTFILE,">>$datafile") || die "Cannot open $datafile :$!"; foreach $data (@data) { print OUTFILE "$data "; } print OUTFILE "\n"; close OUTFILE;
a
|
|---|