2 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
5 # $Id: piconv,v 2.1 2004/10/06 05:07:20 dankogai Exp $
11 my %Scheme = map {$_ => 1} qw(from_to decode_encode perlio);
14 my $name = basename
($0);
16 use Getopt
::Long
qw(:config no_ignore_case);
36 $Opt{help
} and help
();
37 $Opt{list
} and list_encodings
();
38 my $locale = $ENV{LC_CTYPE
} || $ENV{LC_ALL
} || $ENV{LANG
};
39 defined $Opt{resolve
} and resolve_encoding
($Opt{resolve
});
40 $Opt{from
} || $Opt{to
} || help
();
41 my $from = $Opt{from
} || $locale or help
("from_encoding unspecified");
42 my $to = $Opt{to
} || $locale or help
("to_encoding unspecified");
43 $Opt{string
} and Encode
::from_to
($Opt{string
}, $from, $to) and print $Opt{string
} and exit;
44 my $scheme = exists $Scheme{$Opt{Scheme
}} ?
$Opt{Scheme
} : 'from_to';
45 $Opt{check
} ||= $Opt{c
};
46 $Opt{perlqq
} and $Opt{check
} = Encode
::FB_PERLQQ
;
49 my $cfrom = Encode
->getEncoding($from)->name;
50 my $cto = Encode
->getEncoding($to)->name;
58 # we do not use <> (or ARGV) for the sake of binmode()
59 @ARGV or push @ARGV, \
*STDIN
;
61 unless ($scheme eq 'perlio'){
64 my $ifh = ref $argv ?
$argv : undef;
65 $ifh or open $ifh, "<", $argv or next;
67 if ($scheme eq 'from_to'){ # default
69 Encode
::from_to
($_, $from, $to, $Opt{check
});
72 }elsif ($scheme eq 'decode_encode'){ # step-by-step
74 my $decoded = decode
($from, $_, $Opt{check
});
75 my $encoded = encode
($to, $decoded);
78 } else { # won't reach
79 die "$name: unknown scheme: $scheme";
84 binmode STDOUT
=> "raw:encoding($to)";
86 my $ifh = ref $argv ?
$argv : undef;
87 $ifh or open $ifh, "<", $argv or next;
88 binmode $ifh => "raw:encoding($from)";
94 print join("\n", Encode
->encodings(":all")), "\n";
98 sub resolve_encoding
{
99 if (my $alias = Encode
::resolve_alias
($_[0])) {
103 warn "$name: $_[0] is not known to Encode\n";
110 $message and print STDERR
"$name error: $message\n";
111 print STDERR
<<"EOT";
112 $name [-f from_encoding] [-t to_encoding] [-s string] [files...]
114 $name -r encoding_alias
116 lists all available encodings
117 -r,--resolve encoding_alias
118 resolve encoding to its (Encode) canonical name
119 -f,--from from_encoding
120 when omitted, the current locale will be used
122 when omitted, the current locale will be used
124 "string" will be the input instead of STDIN or files
125 The following are mainly of interest to Encode hackers:
126 -D,--debug show debug information
127 -C N | -c | -p check the validity of the input
128 -S,--scheme scheme use the scheme for conversion
137 piconv -- iconv(1), reinvented in perl
141 piconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
151 B<piconv> is perl version of B<iconv>, a character encoding converter
152 widely available for various Unixen today. This script was primarily
153 a technology demonstrator for Perl 5.8.0, but you can use piconv in the
154 place of iconv for virtually any case.
156 piconv converts the character encoding of either STDIN or files
157 specified in the argument and prints out to STDOUT.
159 Here is the list of options. Each option can be in short format (-f)
164 =item -f,--from from_encoding
166 Specifies the encoding you are converting from. Unlike B<iconv>,
167 this option can be omitted. In such cases, the current locale is used.
169 =item -t,--to to_encoding
171 Specifies the encoding you are converting to. Unlike B<iconv>,
172 this option can be omitted. In such cases, the current locale is used.
174 Therefore, when both -f and -t are omitted, B<piconv> just acts
177 =item -s,--string I<string>
179 uses I<string> instead of file for the source of text.
183 Lists all available encodings, one per line, in case-insensitive
184 order. Note that only the canonical names are listed; many aliases
185 exist. For example, the names are case-insensitive, and many standard
186 and common aliases work, such as "latin1" for "ISO-8859-1", or "ibm850"
187 instead of "cp850", or "winlatin1" for "cp1252". See L<Encode::Supported>
188 for a full discussion.
190 =item -C,--check I<N>
192 Check the validity of the stream if I<N> = 1. When I<N> = -1, something
193 interesting happens when it encounters an invalid character.
209 Invokes debugging mode. Primarily for Encode hackers.
211 =item -S,--scheme scheme
213 Selects which scheme is to be used for conversion. Available schemes
220 Uses Encode::from_to for conversion. This is the default.
224 Input strings are decode()d then encode()d. A straight two-step
229 The new perlIO layer is used. NI-S' favorite.
233 Like the I<-D> option, this is also for Encode hackers.