Commit | Line | Data |
---|---|---|
67d7b5ef | 1 | #!/usr/bin/perl |
037b88d6 | 2 | # $Id: piconv,v 1.0 2002/03/28 23:26:28 dankogai Exp $ |
67d7b5ef JH |
3 | # |
4 | use 5.7.3; | |
5 | use strict; | |
6 | use Encode ; | |
7 | use Encode::Alias; | |
8 | my %Scheme = map {$_ => 1} qw(from_to decode_encode perlio); | |
9 | ||
10 | use Getopt::Std; | |
11 | ||
12 | my %Opt; getopts("DS:lf:t:s:", \%Opt); | |
13 | $Opt{l} and list_encodings(); | |
14 | my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG}; | |
15 | my $from = $Opt{f} || $locale or help("from_encoding unspecified"); | |
16 | my $to = $Opt{t} || $locale or help("to_encoding unspecified"); | |
17 | $Opt{s} and Encode::from_to($Opt{s}, $from, $to) and print $Opt{s} and exit; | |
18 | my $scheme = exists $Scheme{$Opt{S}} ? $Opt{S} : 'from_to'; | |
19 | ||
20 | if ($Opt{D}){ | |
21 | my $cfrom = Encode->getEncoding($from)->name; | |
22 | my $cto = Encode->getEncoding($to)->name; | |
23 | print STDERR <<"EOT"; | |
24 | Scheme: $scheme | |
25 | From: $from => $cfrom | |
26 | To: $to => $cto | |
27 | EOT | |
28 | } | |
29 | ||
30 | # default | |
31 | if ($scheme eq 'from_to'){ | |
32 | while(<>){ | |
33 | Encode::from_to($_, $from, $to); print; | |
34 | }; | |
35 | # step-by-step | |
36 | }elsif ($scheme eq 'decode_encode'){ | |
37 | while(<>){ | |
38 | my $decoded = decode($from, $_); | |
39 | my $encoded = encode($to, $decoded); | |
40 | print $encoded; | |
41 | }; | |
42 | # NI-S favorite | |
43 | }elsif ($scheme eq 'perlio'){ | |
44 | binmode(STDIN, ":encoding($from)"); | |
45 | binmode(STDOUT, ":encoding($to)"); | |
46 | while(<>){ print; } | |
47 | }else{ # won't reach | |
48 | die "unknown scheme: $scheme"; | |
49 | } | |
50 | ||
51 | sub list_encodings{ | |
52 | print STDERR join("\n", Encode->encodings(":all")), "\n"; | |
53 | exit; | |
54 | } | |
55 | ||
56 | sub help{ | |
57 | my $message = shift; | |
58 | use File::Basename; | |
59 | my $name = basename($0); | |
60 | $message and print STDERR "$name error: $message\n"; | |
61 | print STDERR <<"EOT"; | |
62 | $name [-f from_encoding] [-t to_encoding] [-s string] [files...] | |
63 | $name -l | |
64 | -l lists all available encodings. | |
65 | -f from_encoding When omitted, the current locale will be used. | |
66 | -t to_encoding When omitted, the current locale will be used. | |
67 | -s string "string" will be converted instead of STDIN. | |
68 | EOT | |
69 | exit; | |
70 | } | |
71 | ||
72 | __END__ | |
73 | ||
74 | =head1 NAME | |
75 | ||
76 | piconv -- iconv(1), reinvented in perl | |
77 | ||
78 | =head1 SYNOPSIS | |
79 | ||
80 | piconv [-f from_encoding] [-t to_encoding] [-s string] [files...] | |
81 | piconv -l | |
82 | ||
83 | =head1 DESCRIPTION | |
84 | ||
85 | B<piconv> is perl version of F<iconv>, a character encoding converter | |
86 | widely availabe for various unixen today. This script was primarily | |
87 | a technology demostrator for Perl 5.8.0, you can use piconv in the | |
88 | place of iconv for virtually any cases. | |
89 | ||
90 | piconv converts character encoding of either STDIN or files specified | |
91 | in the argument and prints out to STDOUT. | |
92 | ||
93 | Here are list of options. | |
94 | ||
95 | =over 4 | |
96 | ||
97 | =item -f from_encoding | |
98 | ||
99 | Specifies the encoding you are converting from. Unlike F<iconv>, | |
100 | this option can be ommited. In such cases the current locale is used. | |
101 | ||
102 | =item -t to_encoding | |
103 | ||
104 | Specifies the encoding you are converting to. Unlike F<iconv>, | |
105 | this option can be ommited. In such cases the current locale is used. | |
106 | ||
107 | Therefore when both -f and -t are omitted, F<piconv> just acts like F<cat>. | |
108 | ||
109 | =item -s I<string> | |
110 | ||
111 | uses I<string> instead of file for the source of text. Same as F<iconv>. | |
112 | ||
113 | =item -l | |
114 | ||
115 | Lists all available encodings to STDERR. This feature is missing from | |
116 | F<iconv>. | |
117 | ||
118 | =item -D | |
119 | ||
120 | Invokes debugging mode. primarily for Encode hackers. | |
121 | ||
122 | =item -S scheme | |
123 | ||
124 | Selects which scheme is to be used for conversion. Available schemes | |
125 | are as follows; | |
126 | ||
127 | =over 4 | |
128 | ||
129 | =item from_to | |
130 | ||
131 | Uses Encode::from_to for conversion. This is the default. | |
132 | ||
133 | =item decode_encode | |
134 | ||
135 | Input strings are decode()ed then encode()ed. A straight step-by-step | |
136 | implementation. | |
137 | ||
138 | =item perlio | |
139 | ||
140 | The new perlIO layer is used. NI-S favorite. | |
141 | ||
142 | =back | |
143 | ||
144 | Like I<-D> option, this is also for Encode hackers. | |
145 | ||
146 | =back | |
147 | ||
148 | =head1 SEE ALSO | |
149 | ||
150 | L<iconv(1)> | |
151 | L<locale(3)> | |
152 | L<Encode> | |
153 | L<PerlIO> | |
154 | ||
155 | =cut |