File Coverage

blib/lib/Encode/Newlines.pm
Criterion Covered Total %
statement 39 40 97.5
branch 13 18 72.2
condition n/a
subroutine 10 11 90.9
pod 1 1 100.0
total 63 70 90.0


line stmt bran cond sub pod time code
1             package Encode::Newlines;
2              
3 1     1   15354 use 5.007003;
  1         4  
  1         57  
4             our $VERSION = '0.04';
5             our $AllowMixed = 0;
6              
7 1     1   6 use strict;
  1         1  
  1         30  
8 1     1   15 use base qw(Encode::Encoding);
  1         2  
  1         105  
9 1     1   5 use constant CR => "\015";
  1         2  
  1         71  
10 1     1   5 use constant LF => "\012";
  1         2  
  1         52  
11 1     1   5 use constant CRLF => "\015\012";
  1         2  
  1         143  
12 1 50       71 use constant Native => (
    50          
13             ($^O =~ /^(?:MSWin|cygwin|dos|os2)/) ? CRLF :
14             ($^O =~ /^MacOS/) ? CR : LF
15 1     1   5 );
  1         2  
16              
17             foreach my $in (qw( CR LF CRLF Native )) {
18             foreach my $out (qw( CR LF CRLF Native )) {
19 1     1   5 no strict 'refs';
  1         2  
  1         90  
20             my $name = (($in eq $out) ? $in : "$in-$out");
21              
22             $Encode::Encoding{$name} = bless {
23             Name => $name,
24             decode => &$in,
25             encode => &$out,
26             } => __PACKAGE__;
27             }
28             };
29              
30             foreach my $func (qw( decode encode )) {
31 1     1   5 no strict 'refs';
  1         1  
  1         364  
32             *$func = sub ($$;$) {
33 14     14   6034 my ($obj, $str, $chk) = @_;
34              
35 14 100       87 if ($AllowMixed) {
    50          
36 1         10 $str =~ s/(?:\015\012?|\012)/$obj->{$func}/g;
37             }
38             elsif ($str =~ /((?:\015\012?|\012))/) {
39 13         29 my $eol = $1;
40              
41 13 100       29 if ($eol eq CRLF) {
42 5         24 require Carp;
43 5 50       32 Carp::croak('Mixed newlines')
44             if $str =~ /\015(?!\012)|(?<!\015)\012/;
45             }
46             else {
47 8         38 require Carp;
48 8 100       361 Carp::croak('Mixed newlines')
    100          
49             if index($str, (($eol eq CR) ? LF : CR)) >= 0;
50             }
51              
52 11         150 $str =~ s/$eol/$obj->{$func}/g;
53             }
54              
55 12 50       33 $_[1] = '' if $chk;
56 12         40 return $str;
57             }
58             }
59              
60 0     0 1   sub perlio_ok { 0 }
61              
62             1;
63             __END__
64              
65             =head1 NAME
66              
67             Encode::Newlines - Normalize line ending sequences
68              
69             =head1 VERSION
70              
71             This document describes version 0.04 of Encode::Newlines, released
72             September 4, 2007.
73              
74             =head1 SYNOPSIS
75              
76             use Encode;
77             use Encode::Newlines;
78              
79             # Convert to native newlines
80             # Note that decode() and encode() are equivalent here
81             $native = decode(Native => $string);
82             $native = encode(Native => $string);
83              
84             {
85             # Allow mixed newlines in $mixed
86             local $Encode::Newlines::AllowMixed = 1;
87             $cr = encode(CR => $mixed);
88             }
89              
90             =head1 DESCRIPTION
91              
92             This module provides the C<CR>, C<LF>, C<CRLF> and C<Native> encodings,
93             to aid in normalizing line endings.
94              
95             It converts whatever line endings the source uses to the designated newline
96             sequence, for both C<encode> and C<decode> operations.
97              
98             If you specify two different line endings joined by a C<->, it will use the
99             first one for decoding and the second one for encoding. For example, the
100             C<LF-CRLF> encoding means that all input should be normalized to C<LF>, and
101             all output should be normalized to C<CRLF>.
102              
103             If the source has an inconsistent line ending style, then a C<Mixed newlines>
104             exception is raised on behalf of the caller. However, if the package variable
105             C<$Encode::Newlines::AllowMixed> is set to a true value, then it will silently
106             convert all three line endings.
107              
108             =head1 CAVEATS
109              
110             This module is not suited for working with L<PerlIO::encoding>, because it
111             cannot guarantee that the chunk bounaries won't happen within a CR/LF
112             sequence. See L<PerlIO::eol> for how to deal with this correctly.
113              
114             An optional XS implemenation would be nice.
115              
116             =head1 AUTHORS
117              
118             Audrey Tang E<lt>audreyt@audreyt.orgE<gt>
119              
120             =head1 COPYRIGHT
121              
122             Copyright 2004-2007 by Audrey Tang E<lt>audreyt@audreyt.orgE<gt>.
123              
124             This program is free software; you can redistribute it and/or
125             modify it under the same terms as Perl itself.
126              
127             See L<http://www.perl.com/perl/misc/Artistic.html>
128              
129             =cut