Например:
% cd src/perl/t
% ls
README comp mro rantests uni
TEST harness op re win32
base io perl run x2p
benchmark japh perl.supp test.pl
cmd lib porting thread_it.pl
% ls -1 | words
README comp mro rantests uni
TEST harness op re win32
base io perl run x2p
benchmark japh perl.supp test.pl
cmd lib porting thread_it.pl
Как видите, скрипт words
представляет свой основной столбец ввода, очень похожий на ls
. Вот его источник.
#!/usr/bin/env perl
#
# words - read input stream, present in column major order to screen size
# Tom Christiansen <tchrist@perl.com>
use strict;
use warnings;
our($elt, $rows, $cols, $xpix, $ypix, $mask, @list);
sub at_eol { ($elt+1) % $cols == 0; } # is this the last elt on line?
my $maxlen = 1; # widest string yet seen
my $winsize = "\0" x 8;
my $TIOCGWINSZ = 0x40087468; # should be require sys/ioctl.pl
if (ioctl(STDOUT, $TIOCGWINSZ, $winsize)) {
($rows, $cols, $xpix, $ypix) = unpack('S4', $winsize);
} else {
($cols) = `stty size 2>&1` =~ /^\d+ (\d+)$/;
$cols ||= 80;
}
my $curlen = 0;
while (<>) { # read stdin into $_
s/\s+\z//;
$maxlen = $curlen if (($curlen = length()) > $maxlen);
push(@list, $_);
}
$maxlen += 1; # spaces
$cols = int($cols / $maxlen) || 1;
$rows = int(($#list+$cols) / $cols);
$mask = sprintf("%%-%ds ", $maxlen);
for ($elt = 0; $elt < $rows * $cols; $elt++) {
my $target = ($elt%$cols) * $rows + int(($elt/$cols));
my $piece = sprintf($mask, $target < ($#list+1) ? $list[$target] : "");
$piece =~ s/\s+$// if at_eol(); # don't blank pad to at_eol of line
print $piece;
print "\n" if at_eol();
}
print "\n" if at_eol();