2 # $Id: piconv,v 2.0 2004/05/16 20:55:16 dankogai Exp $
8 my %Scheme = map {$_ => 1} qw(from_to decode_encode perlio);
11 my $name = basename($0);
13 use Getopt::Long qw(:config no_ignore_case);
33 $Opt{help} and help();
34 $Opt{list} and list_encodings();
35 my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG};
36 defined $Opt{resolve} and resolve_encoding($Opt{resolve});
37 $Opt{from} || $Opt{to} || help();
38 my $from = $Opt{from} || $locale or help("from_encoding unspecified");
39 my $to = $Opt{to} || $locale or help("to_encoding unspecified");
40 $Opt{string} and Encode::from_to($Opt{string}, $from, $to) and print $Opt{string} and exit;
41 my $scheme = exists $Scheme{$Opt{Scheme}} ? $Opt{Scheme} : 'from_to';
42 $Opt{check} ||= $Opt{c};
43 $Opt{perlqq} and $Opt{check} = Encode::FB_PERLQQ;
46 my $cfrom = Encode->getEncoding($from)->name;
47 my $cto = Encode->getEncoding($to)->name;
56 if ($scheme eq 'from_to'){
58 Encode::from_to($_, $from, $to, $Opt{check}); print;
61 }elsif ($scheme eq 'decode_encode'){
63 my $decoded = decode($from, $_, $Opt{check});
64 my $encoded = encode($to, $decoded);
68 }elsif ($scheme eq 'perlio'){
69 binmode(STDIN, ":encoding($from)");
70 binmode(STDOUT, ":encoding($to)");
72 } else { # won't reach
73 die "$name: unknown scheme: $scheme";
77 print join("\n", Encode->encodings(":all")), "\n";
81 sub resolve_encoding {
82 if (my $alias = Encode::resolve_alias($_[0])) {
86 warn "$name: $_[0] is not known to Encode\n";
93 $message and print STDERR "$name error: $message\n";
95 $name [-f from_encoding] [-t to_encoding] [-s string] [files...]
97 $name -r encoding_alias
99 lists all available encodings
100 -r,--resolve encoding_alias
101 resolve encoding to its (Encode) canonical name
102 -f,--from from_encoding
103 when omitted, the current locale will be used
105 when omitted, the current locale will be used
107 "string" will be the input instead of STDIN or files
108 The following are mainly of interest to Encode hackers:
109 -D,--debug show debug information
110 -C N | -c | -p check the validity of the input
111 -S,--scheme scheme use the scheme for conversion
120 piconv -- iconv(1), reinvented in perl
124 piconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
134 B<piconv> is perl version of B<iconv>, a character encoding converter
135 widely available for various Unixen today. This script was primarily
136 a technology demonstrator for Perl 5.8.0, but you can use piconv in the
137 place of iconv for virtually any case.
139 piconv converts the character encoding of either STDIN or files
140 specified in the argument and prints out to STDOUT.
142 Here is the list of options. Each option can be in short format (-f)
147 =item -f,--from from_encoding
149 Specifies the encoding you are converting from. Unlike B<iconv>,
150 this option can be omitted. In such cases, the current locale is used.
152 =item -t,--to to_encoding
154 Specifies the encoding you are converting to. Unlike B<iconv>,
155 this option can be omitted. In such cases, the current locale is used.
157 Therefore, when both -f and -t are omitted, B<piconv> just acts
160 =item -s,--string I<string>
162 uses I<string> instead of file for the source of text.
166 Lists all available encodings, one per line, in case-insensitive
167 order. Note that only the canonical names are listed; many aliases
168 exist. For example, the names are case-insensitive, and many standard
169 and common aliases work, such as "latin1" for "ISO-8859-1", or "ibm850"
170 instead of "cp850", or "winlatin1" for "cp1252". See L<Encode::Supported>
171 for a full discussion.
173 =item -C,--check I<N>
175 Check the validity of the stream if I<N> = 1. When I<N> = -1, something
176 interesting happens when it encounters an invalid character.
192 Invokes debugging mode. Primarily for Encode hackers.
194 =item -S,--scheme scheme
196 Selects which scheme is to be used for conversion. Available schemes
203 Uses Encode::from_to for conversion. This is the default.
207 Input strings are decode()d then encode()d. A straight two-step
212 The new perlIO layer is used. NI-S' favorite.
216 Like the I<-D> option, this is also for Encode hackers.