Any reason you're declaring all those variables up front, one per line? Also, why spawn a process for a grep when Perl can do that with the same amount of effort?
#!/usr/bin/perl -w
use strict;
my @info = grep /:/, do {
open my $pipe, "smbstatus -b|"
or die "Couldn't spawn 'smbstatus': $!\n";
<$pipe>;
};
for(@info) {
my ($pid, $loginname, $machine, $dow, $mon, $day, $time, $year)
= split /\s+/, $_, 8;
print "$loginname logged in on machine: $machine (login: $mon $day
+ - $time)\n";
}
print "\ntotal users: " . @info . "\n";
Or maybe even
#!/usr/bin/perl -w
use strict;
use constant PID => 0;
use constant LOGIN => 1;
use constant MACHINE => 2;
use constant WEEKDAY => 3;
use constant MONTH => 4;
use constant DAY => 5;
use constant TIME => 6;
use constant YEAR => 7;
use constant INFOFMT => "%s logged in on machine: %s (login: %s %s - %
+s)\n";
use constant INFOFIELDS => (LOGIN, MACHINE, MONTH, DAY, TIME);
my @info = grep /:/, do {
open my $pipe, "smbstatus -b|"
or die "Couldn't spawn 'smbstatus': $!\n";
<$pipe>;
};
printf INFOFMT, (split /\s+/, $_, 8)[INFOFIELDS] for @info;
print "\ntotal users: " . @info . "\n";
I actually like the second version a lot more, code wise, but wish it was possible to reduce the verbosity of the constant declarations..
Update: s/\bMON\b/MONTH/
Makeshifts last the longest. |