#!/usr/bin/perl
use
Encode
qw[ decode_utf8 ]
;
if
(
$o
{v} ) {
eval
'use Text::VisualWidth::UTF8 qw[ trim width ] ; 1 '
or
die
'Installing Text::VisualWidth::UTF8 is necessary.'
};
sub
trim ( $$ ) {
substr
(
$_
[0], 0 ,
$_
[1] ) }
no
warnings ;
* trim = * Text::VisualWidth::UTF8::trim
if
$o
{v} ;
$| = 1
if
$o
{
'!'
} ;
my
$size
;
my
$cont
=
$o
{
'.'
} //
'..'
;
my
$iosep
=
$o
{
'/'
} //
"\t"
;
& Init ;
& main ;
exit
;
sub
Line ( $ @ ) {
my
@cells
;
for
(
@_
[1..
$#_
] ) {
push
@cells
,
undef
and
next
if
!
defined
$_
;
my
$str
= trim (
$_
,
$size
) ;
my
$q
=
quotemeta
$str
;
s/^
$q
// ;
$_
=
undef
if
m/^$/ ;
push
@cells
,
$str
. (
defined
$_
?
$cont
:
''
) ;
}
unshift
@cells
,
"$_[0]"
if
$o
{
':'
} ;
print
join
$iosep
,
map
{
$_
//
''
}
@cells
;
print
"\n"
;
}
sub
main ( ) {
while
( <> ) {
chomp
;
my
@F
=
split
/
$iosep
/ ,
$_
, -1 ;
my
$m
=
$o
{y} // 3 ;
my
$v
=
"$.:"
;
do
{ Line (
$v
,
@F
) ;
$v
=
''
}
while
( any {
defined
$_
}
@F
) ;
}
}
sub
Init ( ) {
binmode
STDIN,
":encoding(utf8)"
if
!
$o
{w} &&
$o
{u} ;
binmode
STDIN,
":encoding(cp932)"
if
$o
{w} ;
binmode
STDOUT,
":encoding(utf8)"
if
$o
{u};
$o
{u} = 3
unless
defined
$o
{b} ||
defined
$o
{u} ||
defined
$o
{v} ;
$size
=
$o
{b} //
$o
{u} //
$o
{v} // 0 ;
}
sub
VERSION_MESSAGE {}
sub
HELP_MESSAGE {
$ARGV
[1] //=
''
;
open
my
$FH
,
'<'
, $0 ;
while
(<
$FH
>){
s/\$0/
$Script
/g ;
print
$_
if
s/^=head1// .. s/^=cut// and
$ARGV
[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
}
close
$FH
;
exit
0 ;
}