summaryrefslogtreecommitdiff
path: root/ext/Encode/bin/piconv
blob: b70a1a801f2bf491ddfc78aebe935e3f88b52828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!./perl
# $Id: piconv,v 1.21 2002/04/09 20:06:15 dankogai Exp $
#
use 5.7.3;
use strict;
use Encode ;
use Encode::Alias;
my %Scheme =  map {$_ => 1} qw(from_to decode_encode perlio);

use Getopt::Std;

my %Opt; getopts("hDS:lf:t:s:", \%Opt);
$Opt{h} and help();
$Opt{l} and list_encodings();
my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG};
$Opt{f} || $Opt{t} || help();
my $from = $Opt{f} || $locale or help("from_encoding unspecified");
my $to   = $Opt{t} || $locale or help("to_encoding unspecified");
$Opt{s} and Encode::from_to($Opt{s}, $from, $to) and print $Opt{s} and exit;
my $scheme = exists $Scheme{$Opt{S}} ? $Opt{S} :  'from_to';

if ($Opt{D}){
    my $cfrom = Encode->getEncoding($from)->name;
    my $cto   = Encode->getEncoding($to)->name;
    print <<"EOT";
Scheme: $scheme
From:   $from => $cfrom
To:     $to => $cto
EOT
}

# default
if     ($scheme eq 'from_to'){ 
    while(<>){
	Encode::from_to($_, $from, $to); print;
    };
# step-by-step
}elsif ($scheme eq 'decode_encode'){
   while(<>){
       my $decoded = decode($from, $_);
       my $encoded = encode($to, $decoded);
       print $encoded;
    };
# NI-S favorite
}elsif ($scheme eq 'perlio'){ 
    binmode(STDIN,  ":encoding($from)");
    binmode(STDOUT, ":encoding($to)");
    while(<>){ print; }
}else{ # won't reach
    die "unknown scheme: $scheme";
}

sub list_encodings{
    print join("\n", Encode->encodings(":all")), "\n";
    exit;
}

sub help{
    my $message = shift;
    use File::Basename;
    my $name = basename($0);
    $message and print STDERR "$name error: $message\n";
    print STDERR <<"EOT";
$name [-f from_encoding] [-t to_encoding] [-s string] [files...]
$name -l
  -l lists all available encodings (the canonical names, many aliases exist)
  -f from_encoding  When omitted, the current locale will be used.
  -t to_encoding    When omitted, the current locale will be used.
  -s string         "string" will be converted instead of STDIN.
EOT
  exit;
}

__END__

=head1 NAME

piconv -- iconv(1), reinvented in perl

=head1 SYNOPSIS

  piconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
  piconv -l

=head1 DESCRIPTION

B<piconv> is perl version of F<iconv>, a character encoding converter
widely available for various Unixen today.   This script was primarily
a technology demonstrator for Perl 5.8.0, you can use piconv in the
place of iconv for virtually any cases.

piconv converts character encoding of either STDIN or files specified
in the argument and prints out to STDOUT.

Here are list of options.

=over 4

=item -f from_encoding

Specifies the encoding you are converting from.  Unlike F<iconv>,
this option can be omitted.  In such cases the current locale is used.

=item -t to_encoding

Specifies the encoding you are converting to.  Unlike F<iconv>,
this option can be omitted.  In such cases the current locale is used.

Therefore when both -f and -t are omitted, F<piconv> just acts like F<cat>.

=item -s I<string>

uses I<string> instead of file for the source of text.  Same as F<iconv>.

=item -l

Lists all available encodings, one per line, in case-insensitive
order.  Note that only the canonical names are listed, many aliases
exist.  For example, the names are case-insensitive, and many standard
and common aliases work, like "latin1" for "ISO 8859-1", or "ibm850"
instead of "cp850", or "winlatin1" for "cp1252".  See L<Encode::Supported>
for the full discussion.

=item -h

Show usage.

=item -D

Invokes debugging mode.  Primarily for Encode hackers.

=item -S scheme

Selects which scheme is to be used for conversion.  Available schemes
are as follows;

=over 4

=item from_to

Uses Encode::from_to for conversion.  This is the default.

=item decode_encode

Input strings are decode()d then encode()d.  A straight two-step
implementation.

=item perlio

The new perlIO layer is used.  NI-S' favorite.

=back

Like I<-D> option, this is also for Encode hackers.

=back

=head1 SEE ALSO

L<iconv(1)>
L<locale(3)>
L<Encode>
L<Encode::Supported>
L<Encode::Alias>
L<PerlIO>

=cut