This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Encode 1.01, from Dan Kogai.
[perl5.git] / ext / Encode / bin / piconv
CommitLineData
67d7b5ef 1#!/usr/bin/perl
037b88d6 2# $Id: piconv,v 1.0 2002/03/28 23:26:28 dankogai Exp $
67d7b5ef
JH
3#
4use 5.7.3;
5use strict;
6use Encode ;
7use Encode::Alias;
8my %Scheme = map {$_ => 1} qw(from_to decode_encode perlio);
9
10use Getopt::Std;
11
12my %Opt; getopts("DS:lf:t:s:", \%Opt);
13$Opt{l} and list_encodings();
14my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG};
15my $from = $Opt{f} || $locale or help("from_encoding unspecified");
16my $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;
18my $scheme = exists $Scheme{$Opt{S}} ? $Opt{S} : 'from_to';
19
20if ($Opt{D}){
21 my $cfrom = Encode->getEncoding($from)->name;
22 my $cto = Encode->getEncoding($to)->name;
23 print STDERR <<"EOT";
24Scheme: $scheme
25From: $from => $cfrom
26To: $to => $cto
27EOT
28}
29
30# default
31if ($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
51sub list_encodings{
52 print STDERR join("\n", Encode->encodings(":all")), "\n";
53 exit;
54}
55
56sub 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.
68EOT
69 exit;
70}
71
72__END__
73
74=head1 NAME
75
76piconv -- 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
85B<piconv> is perl version of F<iconv>, a character encoding converter
86widely availabe for various unixen today. This script was primarily
87a technology demostrator for Perl 5.8.0, you can use piconv in the
88place of iconv for virtually any cases.
89
90piconv converts character encoding of either STDIN or files specified
91in the argument and prints out to STDOUT.
92
93Here are list of options.
94
95=over 4
96
97=item -f from_encoding
98
99Specifies the encoding you are converting from. Unlike F<iconv>,
100this option can be ommited. In such cases the current locale is used.
101
102=item -t to_encoding
103
104Specifies the encoding you are converting to. Unlike F<iconv>,
105this option can be ommited. In such cases the current locale is used.
106
107Therefore when both -f and -t are omitted, F<piconv> just acts like F<cat>.
108
109=item -s I<string>
110
111uses I<string> instead of file for the source of text. Same as F<iconv>.
112
113=item -l
114
115Lists all available encodings to STDERR. This feature is missing from
116F<iconv>.
117
118=item -D
119
120Invokes debugging mode. primarily for Encode hackers.
121
122=item -S scheme
123
124Selects which scheme is to be used for conversion. Available schemes
125are as follows;
126
127=over 4
128
129=item from_to
130
131Uses Encode::from_to for conversion. This is the default.
132
133=item decode_encode
134
135Input strings are decode()ed then encode()ed. A straight step-by-step
136implementation.
137
138=item perlio
139
140The new perlIO layer is used. NI-S favorite.
141
142=back
143
144Like I<-D> option, this is also for Encode hackers.
145
146=back
147
148=head1 SEE ALSO
149
150L<iconv(1)>
151L<locale(3)>
152L<Encode>
153L<PerlIO>
154
155=cut