in reply to efficiency & style
I was even planning a comment about sprintf/localtime! The only difference is the way we did the loop.
#!/usr/bin/perl -w use strict; my %services = (www => qr/:0050\s/, ssh => qr/:0016\s/, irc => qr/:1A0B\s/, ftp => qr/:0015\s/, ); my $date = `date +"%l:%M:%S"`; my @tcpalive = grep /\b01\s/,`cat /proc/net/tcp`; while (my($key,$REx) = each %services) { $services{$key} = grep /$REx/,@tcpalive } open (FILE, "+>/var/www/web/main.txt") or die $!; print FILE "There are $services{www} web, $services{ftp} ftp, ", "$services{ssh} ssh, and $services{irc} irc connections ", "to this server as of $date\n"; close FILE or die $!;
In general, `grep' is your friend. What you had was not at all bad, though, just a little un-Perlish. Seeing -w and strict warms my heart :-)
|
---|