File Coverage

blib/lib/Filter/Encoding.pm
Criterion Covered Total %
statement 21 25 84.0
branch 6 8 75.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 32 40 80.0


line stmt bran cond sub pod time code
1             package Filter::Encoding;
2              
3             $VERSION = '0.01';
4              
5 1     1   838 use utf8 ();
  1         7  
  1         22  
6 1     1   440 use Filter::Util::Call qw 'filter_add filter_read';
  1         681  
  1         50  
7 1     1   468 use Encode 'find_encoding';
  1         7324  
  1         199  
8              
9             sub dy {
10 0     0 0 0 require Carp;
11 0         0 goto &Carp::croak;
12             }
13              
14             sub import {
15 2     2   9 shift;
16 2 100       25 return unless @_;
17              
18 1 50       3 unless (@_ == 1) {
19 0         0 dy "Too many arguments to Filter::Encoding->import()";
20             }
21 1         3 my $enc = find_encoding($_[0]);
22 1 50       1805 unless ( defined $enc ) {
23 0         0 dy __PACKAGE__.": Unknown encoding '$_[0]'";
24             }
25              
26 1         5 import utf8;
27             filter_add(
28             sub {
29 21     21   3985 my $status = filter_read();
30 21 100       33 if ( $status > 0 ) {
31 20         46 $_ = $enc->decode( $_, 1 );
32              
33             # Currently does nothing, but if perl switches to a saner
34             # model this may become necessary.
35 20         21 utf8'encode $_;
36             }
37 21         1184 $status;
38             }
39 1         7 );
40             _:
41             }
42              
43             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!()__END__()!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44              
45             =head1 NAME
46              
47             Filter::Encoding - Write your script in any encoding
48              
49             =head1 VERSION
50              
51             Version 0.01
52              
53             =head1 SYNOPSIS
54              
55             use Filter::Encoding 'MacRoman';
56             # Code that follows can be written in MacRoman encoding.
57              
58             =head1 DESCRIPTION
59              
60             This module allows your code to be written in any ASCII-based encoding.
61             Just pass the name of the encoding as an argument to C
62             Filter::Encoding>. The source code will be decoded and treated as though it had been written in UTF-8 with C in effect. That's all this
63             module does.
64              
65             It is intended as a simpler, saner replacement for L,
66             one that does not change the up- and downgrading of strings or touch your
67             file handles.
68              
69             =head1 DIAGNOSTICS
70              
71             =item Too many arguments to Filter::Encoding->import()
72              
73             C (which implies C<< ->import >>) only allows one
74             argument.
75              
76             =item Filter::Encoding: Unknown encoding '%s'
77              
78             The encoding must be one recognized by the C module.
79              
80             =head1 PREREQUISITES
81              
82             perl 5.8.0 or later
83              
84             =head1 AUTHOR & COPYRIGHT
85              
86             Copyright (C) 2016 Father Chrysostomos
87             [dot] org>
88              
89             This program is free software; you may redistribute it, modify it, or both
90             under the same terms as perl.
91              
92             =head1 ACKNOWLEDGEMENTS
93              
94             Much of the code was based on the filter feature of
95             L.
96              
97             =head1 SEE ALSO
98              
99             L, L