hostyle has asked for the wisdom of the Perl Monks concerning the following question:
I'm by no means a great perl programmer, although I've improved every year thus far. I had a quick and dirty code competition with some friends to write the shortest (legible) perl version of the tcsh 'watch' command. I won with the following, but I'm sure it can be shortened further. I'd like to know how (and preferably with it kept at least semi-legible).
#!/usr/bin/perl -w use strict; my (%users, @in); while (<DATA>) { chomp; $users{$_} = 0; } for (;;) { @in = split/ /,`users`; foreach my $n (keys %users) { if ( !$users{$n} && map {/^$n$/} @in ) { $users{$n} = 1; print scalar localtime, " $n has logged in \n"; } if ( $users{$n} && ! map {/^$n$/} @in ) { $users{$n} = 0; print scalar localtime, " $n has logged out \n"; } } sleep 2; } __DATA__ list of users to watch
20040812 Edit by ysth: change pre tags to code tags
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Shorten this code
by ccn (Vicar) on Aug 12, 2004 at 21:19 UTC | |
by tilly (Archbishop) on Aug 13, 2004 at 01:37 UTC | |
by hostyle (Scribe) on Aug 13, 2004 at 17:44 UTC | |
by BrowserUk (Patriarch) on Aug 13, 2004 at 19:06 UTC | |
|
Re: Shorten this code
by blokhead (Monsignor) on Aug 12, 2004 at 21:01 UTC | |
|
Re: Shorten this code
by BrowserUk (Patriarch) on Aug 12, 2004 at 21:44 UTC | |
|
Re: Shorten this code
by Aristotle (Chancellor) on Aug 13, 2004 at 02:52 UTC |