使用后缀“|”操作实现从标准输入设备中读取内容。
#!/usr/bin/perl
use strict;
my %who;
open(WHOFH,"who |") or die "can not open who:$!";
while(<WHOFH>){
next unless /^(/S+)/;
$who{$1}++;
}
foreach (sort{$who{$b} <=> $who{$a}} keys %who){
printf "%10s %d/n",$_,$who{$_};
}
close WHOFH or die "close error:$!";
在Linux运行结果
landopen@server01:~$ perl whos_there.pl
landopen 1
dinwen 1