#!/usr/bin/perl
use
Getopt::Std ; getopts
':.:/:!bg:h:uvwy:'
=> \
my
%o
;
use
Encode
qw[ decode_utf8 ]
;
eval
'use Text::VisualWidth::UTF8 qw[trim width]; 1'
or
die
'Be Text::VisualWidth::UTF8 installed.'
if
$o
{v} ;
* trim = * Text::VisualWidth::UTF8::trim
if
$o
{v} ;
* trim =
$o
{v} ? * Text::VisualWidth::UTF8::trim :
sub
{
substr
(
$_
[0], 0 ,
$_
[1] ) } ;
$| = 1
if
$o
{
'!'
} ;
$o
{h} //= 0 ;
my
(
$tlen
,
$tmax
) =
do
{ (
$o
{g}//=
''
) =~ m/(\d*)\D?(\d*)/ ; ( $1 || 6 , $2 // 2 ||
"Inf"
) } ;
my
$existNext
=
$o
{
'.'
} //
'.'
;
my
$iosep
=
$o
{
'/'
} //
"\t"
;
binmode
STDIN,
":encoding(utf8)"
if
!
$o
{w} &&
$o
{u} ;
binmode
STDIN,
":encoding(cp932)"
if
$o
{w} ;
binmode
STDOUT,
":encoding(utf8)"
if
$o
{u} ||
$o
{w} ;
& main ;
exit
;
sub
aLinOut ( @ ) {
my
@cells
;
for
( 1 ..
$#_
) { # 各マスの文字列について.. (
$_
は1始まりで左から何番目かを表す )
push
@cells
,
undef
and
next
if
!
defined
$_
[
$_
] ;
my
$str
=
$_
<=
$o
{h} ?
$_
[
$_
] : trim (
$_
[
$_
] ,
$tlen
) ;
$_
[
$_
] =~ s/^\Q
$str
\E// ;
$_
[
$_
] =
undef
if
$_
[
$_
] =~ m/^$/ ;
push
@cells
,
$str
. (
defined
$_
[
$_
] ?
$existNext
:
''
) ;
}
unshift
@cells
,
"$_[0]"
if
defined
$_
[0] ;
say
join
$iosep
,
map
{
$_
//
''
}
@cells
;
}
sub
main ( ) {
while
( <> ) {
chomp
;
my
$lc
=
"$.:"
if
$o
{
':'
} ;
my
@F
=
split
/
$iosep
/o ,
$_
, -1 ;
for
(
my
$t
=0 ; ++
$t
<=
$tmax
; ) { aLinOut
$lc
,
@F
;
last
if
! any {
defined
$_
}
@F
}
}
}
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 ;
}