Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Reaped: Testing the network connection

by NodeReaper (Curate)
on Jan 28, 2001 at 15:57 UTC ( [id://54844]=perlquestion: print w/replies, xml ) Need Help??

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

This node was taken out by the NodeReaper on May 26, 2021 at 21:47 UTC

You may view the original node and the consideration vote tally.

Replies are listed 'Best First'.
Re: Testing the network connection
by a (Friar) on Jan 29, 2001 at 10:32 UTC
    Okay, w/o questioning the idea, some code comments:
    Well #1, as always "use strict". You'll be busted 9 ways from Sunday here, but cleaning them up will help you alot. But:
    foreach(@sites){ $site=$_; # call the sub &getpages($site); }
    seems:
    foreach my $site ( @sites) { &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:
    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
    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:
    if ( $debug > 10 ) { print STDERR "Site $site took $elapased seconds to load\n"; }
    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:
    $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;
    As for the next part (whoof, I'm tired), well you should probably be sure $samples isn't zero, er.

    a

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-23 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found