in reply to suggestions for improvement
Second suggestion: The regex line in cpu_stat can be shortened... Since a m// regex returns ($1,$2,$3,etc) when called in list context, you can shorten that line to:
Third suggestion, since you aren't actually doing anything with the individual values in the subroutine, don't even bother with multiple scalars... just use an array. Fourth: while seems overkill since you aren't really looping. Just grab one line w/o a loop.($cpu_user, $cpu_nice, $cpu_total, $cpu_idle) = /^cpu\s+(\d+)\ (\d+) ( +\d+) (\d+)/;
Fifth: you can also remove the backslash before the space in your regex...
sub cpu_stats { my @cpustats; open STAT_FILE, "/proc/stat" or warn "Couldn't open /proc/stat: $!\ +n"; my $line = <STAT_FILE>; @cpustats = $line =~ /^cpu\s+(\d+) (\d+) (\d+) (\d+)/; close STAT_FILE; return(@cpustats); }
-Blake
|
|---|