[+] Perl, formatowanie tekstu w konsoli - coś typu tabela
: 11 sierpnia 2012, 03:37
Mam taki kod:
Teoretycznie kod powinien wypisywać coś w tym stylu:
Jednak nie wiem dlaczego wynik jest zupełnie inny:
Kod: Zaznacz cały
#!/usr/bin/env perl
#----------------------------------------------------------------------
# /usr/local/bin/update.pl
#
# v0.1.1
#----------------------------------------------------------------------
# Modules
#
use strict;
use warnings;
use Linux::APT;
#----------------------------------------------------------------------
my $APT = Linux::APT->new(
debug => 0
);
my $l = 0;
my $len = 0;
my $toupgrade = $APT->toupgrade;
my @up_pkgs = $toupgrade->{packages};
my $output = " PACKAGE | CURRENT | NEW
-------------------------+--------------------+--------------------\n";
for my $i (@up_pkgs) {
while(my($package, $versions) = each %{$i}) {
# length of the package name
$len = length $package;
$l = $len-25;
$output = $output . sprintf(" %${l}s", $package);
$output = $output . "|";
$len = $l = 0;
# length of the package current version
$len = length $versions->{current};
$l = $len-20;
$output = $output . sprintf(" %${l}s", $versions->{current});
$output = $output . "|";
$len = $l = 0;
# length of the package new version
$len = length $versions->{new};
$l = $len-20;
$output = $output . sprintf(" %${l}s", $versions->{new});
$output = $output . "\n";
}
}
$output = $output . "-------------------------+--------------------+--------------------\n";
print $output;
#----------------------------------------------------------------------
exit 0;
Teoretycznie kod powinien wypisywać coś w tym stylu:
Kod: Zaznacz cały
PACKAGE | CURRENT | NEW
-------------------------+--------------------+--------------------
mysql-client-5.5 | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
libss2 | 1.42.4-3 | 1.42.5-1
libmysqlclient18 | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
libcomerr2 | 1.42.4-3 | 1.42.5-1
e2fslibs | 1.42.4-3 | 1.42.5-1
mysql-server-5.5 | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
e2fsprogs | 1.42.4-3 | 1.42.5-1
mysql-server-core-5.5 | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
apt | 0.8.15.10 | 0.9.7.2
mysql-client | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
mysql-server | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
mysql-common | 5.5.24+dfsg-5 | 5.5.24+dfsg-6
-------------------------+--------------------+--------------------
Jednak nie wiem dlaczego wynik jest zupełnie inny:
Kod: Zaznacz cały
PACKAGE | CURRENT | NEW
-------------------------+--------------------+--------------------
mysql-client-5.5| 5.5.24+dfsg-5| 5.5.24+dfsg-6
libss2 | 1.42.4-3 | 1.42.5-1
libmysqlclient18| 5.5.24+dfsg-5| 5.5.24+dfsg-6
libcomerr2 | 1.42.4-3 | 1.42.5-1
e2fslibs | 1.42.4-3 | 1.42.5-1
mysql-server-5.5| 5.5.24+dfsg-5| 5.5.24+dfsg-6
e2fsprogs | 1.42.4-3 | 1.42.5-1
mysql-server-core-5.5| 5.5.24+dfsg-5| 5.5.24+dfsg-6
apt | 0.8.15.10 | 0.9.7.2
mysql-client | 5.5.24+dfsg-5| 5.5.24+dfsg-6
mysql-server | 5.5.24+dfsg-5| 5.5.24+dfsg-6
mysql-common | 5.5.24+dfsg-5| 5.5.24+dfsg-6
-------------------------+--------------------+--------------------