home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / bin / piconv.bat < prev    next >
Encoding:
DOS Batch File  |  2002-12-01  |  5.0 KB  |  203 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15.     eval 'exec D:\p4\Apps\Gecko\MSI\data\ActivePerl\Perl\bin\perl.exe -S $0 ${1+"$@"}'
  16.     if $running_under_some_shell;
  17. #!./perl
  18. # $Id: piconv,v 1.25 2002/06/01 18:07:49 dankogai Exp $
  19. #
  20. use 5.8.0;
  21. use strict;
  22. use Encode ;
  23. use Encode::Alias;
  24. my %Scheme =  map {$_ => 1} qw(from_to decode_encode perlio);
  25.  
  26. use Getopt::Std;
  27.  
  28. my %Opt; getopts("pcC:hDS:lf:t:s:", \%Opt);
  29. $Opt{h} and help();
  30. $Opt{l} and list_encodings();
  31. my $locale = $ENV{LC_CTYPE} || $ENV{LC_ALL} || $ENV{LANG};
  32. $Opt{f} || $Opt{t} || help();
  33. my $from = $Opt{f} || $locale or help("from_encoding unspecified");
  34. my $to   = $Opt{t} || $locale or help("to_encoding unspecified");
  35. $Opt{s} and Encode::from_to($Opt{s}, $from, $to) and print $Opt{s} and exit;
  36. my $scheme = exists $Scheme{$Opt{S}} ? $Opt{S} :  'from_to';
  37. $Opt{C} ||= $Opt{c};
  38. $Opt{p} and $Opt{C} = Encode::FB_PERLQQ;
  39.  
  40. if ($Opt{D}){
  41.     my $cfrom = Encode->getEncoding($from)->name;
  42.     my $cto   = Encode->getEncoding($to)->name;
  43.     print <<"EOT";
  44. Scheme: $scheme
  45. From:   $from => $cfrom
  46. To:     $to => $cto
  47. EOT
  48. }
  49.  
  50. # default
  51. if     ($scheme eq 'from_to'){ 
  52.     while(<>){
  53.     Encode::from_to($_, $from, $to, $Opt{C}); print;
  54.     };
  55. # step-by-step
  56. }elsif ($scheme eq 'decode_encode'){
  57.    while(<>){
  58.        my $decoded = decode($from, $_, $Opt{C});
  59.        my $encoded = encode($to, $decoded);
  60.        print $encoded;
  61.     };
  62. # NI-S favorite
  63. }elsif ($scheme eq 'perlio'){ 
  64.     binmode(STDIN,  ":encoding($from)");
  65.     binmode(STDOUT, ":encoding($to)");
  66.     while(<>){ print; }
  67. }else{ # won't reach
  68.     die "unknown scheme: $scheme";
  69. }
  70.  
  71. sub list_encodings{
  72.     print join("\n", Encode->encodings(":all")), "\n";
  73.     exit;
  74. }
  75.  
  76. sub help{
  77.     my $message = shift;
  78.     use File::Basename;
  79.     my $name = basename($0);
  80.     $message and print STDERR "$name error: $message\n";
  81.     print STDERR <<"EOT";
  82. $name [-f from_encoding] [-t to_encoding] [-s string] [files...]
  83. $name -l
  84.   -l lists all available encodings (the canonical names, many aliases exist)
  85.   -f from_encoding  When omitted, the current locale will be used.
  86.   -t to_encoding    When omitted, the current locale will be used.
  87.   -s string         "string" will be converted instead of STDIN.
  88. EOT
  89.   exit;
  90. }
  91.  
  92. __END__
  93.  
  94. =head1 NAME
  95.  
  96. piconv -- iconv(1), reinvented in perl
  97.  
  98. =head1 SYNOPSIS
  99.  
  100.   piconv [-f from_encoding] [-t to_encoding] [-s string] [files...]
  101.   piconv -l
  102.  
  103. =head1 DESCRIPTION
  104.  
  105. B<piconv> is perl version of B<iconv>, a character encoding converter
  106. widely available for various Unixen today.  This script was primarily
  107. a technology demonstrator for Perl 5.8.0, but you can use piconv in the
  108. place of iconv for virtually any case.
  109.  
  110. piconv converts the character encoding of either STDIN or files
  111. specified in the argument and prints out to STDOUT.
  112.  
  113. Here is the  list of options.
  114.  
  115. =over 4
  116.  
  117. =item -f from_encoding
  118.  
  119. Specifies the encoding you are converting from.  Unlike B<iconv>,
  120. this option can be omitted.  In such cases, the current locale is used.
  121.  
  122. =item -t to_encoding
  123.  
  124. Specifies the encoding you are converting to.  Unlike B<iconv>,
  125. this option can be omitted.  In such cases, the current locale is used.
  126.  
  127. Therefore, when both -f and -t are omitted, B<piconv> just acts
  128. like B<cat>.
  129.  
  130. =item -s I<string>
  131.  
  132. uses I<string> instead of file for the source of text.  Same as B<iconv>.
  133.  
  134. =item -l
  135.  
  136. Lists all available encodings, one per line, in case-insensitive
  137. order.  Note that only the canonical names are listed; many aliases
  138. exist.  For example, the names are case-insensitive, and many standard
  139. and common aliases work, such as "latin1" for "ISO-8859-1", or "ibm850"
  140. instead of "cp850", or "winlatin1" for "cp1252".  See L<Encode::Supported>
  141. for a full discussion.
  142.  
  143. =item -C I<N>
  144.  
  145. Check the validity of the stream if I<N> = 1.  When I<N> = -1, something
  146. interesting happens when it encounters an invalid character.
  147.  
  148. =item -c
  149.  
  150. Same as C<-C 1>.
  151.  
  152. =item -p
  153.  
  154. Same as C<-C -1>.
  155.  
  156. =item -h
  157.  
  158. Show usage.
  159.  
  160. =item -D
  161.  
  162. Invokes debugging mode.  Primarily for Encode hackers.
  163.  
  164. =item -S scheme
  165.  
  166. Selects which scheme is to be used for conversion.  Available schemes
  167. are as follows:
  168.  
  169. =over 4
  170.  
  171. =item from_to
  172.  
  173. Uses Encode::from_to for conversion.  This is the default.
  174.  
  175. =item decode_encode
  176.  
  177. Input strings are decode()d then encode()d.  A straight two-step
  178. implementation.
  179.  
  180. =item perlio
  181.  
  182. The new perlIO layer is used.  NI-S' favorite.
  183.  
  184. =back
  185.  
  186. Like the I<-D> option, this is also for Encode hackers.
  187.  
  188. =back
  189.  
  190. =head1 SEE ALSO
  191.  
  192. L<iconv(1)>
  193. L<locale(3)>
  194. L<Encode>
  195. L<Encode::Supported>
  196. L<Encode::Alias>
  197. L<PerlIO>
  198.  
  199. =cut
  200.  
  201. __END__
  202. :endofperl
  203.