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.


In reply to Re: Using smbstatusinfo in scripts (improved) by Aristotle
in thread Using smbstatusinfo in scripts by teabag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.