in reply to Shorten this code

#!/usr/bin/perl -w0777 @users{split /\s+/, <DATA>}=(); while(sleep 2) { for my $n (keys %users) { $_ = `users`; $state = ''; $users{$n} = 1, $state = 'in' if !$users{$n} && /\b$n\b/; $users{$n} = 0, $state = 'out' if $users{$n} && !/\b$n\b/; print localtime() . " $n has logged $state\n" if $state; } }

Replies are listed 'Best First'.
Re^2: Shorten this code
by tilly (Archbishop) on Aug 13, 2004 at 01:37 UTC
    Let's get rid of the $state variable.
    #!/usr/bin/perl -l -w0777 @users{split /\s+/, <DATA>}=(); while(sleep 2) { $_ = `users`; for my $n (keys %users) { if ($users{$n} xor /\b$n\b/) { $users{$n} = /\b$n\b/; print localtime() . " $n has logged ", $users{$n} ? "in" : "out" +; } } }
    UPDATE: There are a number of golf tricks that I've left out. For instance ("out", "in")[$users{$n} = /\b$n\b/] manages to both remember and select the current state, removing one line. But readability suffers.

      Thanks everyone for your contributions. I have only one main question:

      #!/usr/bin/perl -l -w0777

      Whats the 0777 do? And has it anything to do with not using strict?