/usr/share/perl5/File/LocalizeNewlines.pm is in libfile-localizenewlines-perl 1.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | package File::LocalizeNewlines;
=pod
=head1 NAME
File::LocalizeNewlines - Localize the newlines for one or more files
=head1 DESCRIPTION
For people that routinely work with a mixture of different platforms
that have conflicting newline formats (mainly *NIX and Win32) there
are a number of different situations that can result in files having
their newlines get corrupted.
File::LocalizeNewlines provides a mechanism for one off or bulk
detection and conversion of these files to the newline style for the
local platform.
The module implements the conversion using a standard "universal line
seperator" regex, which ensures that files with any of the different
newlines, plus a couple of common "broken" newlines, including
multiple different types mixed in the same file, are all converted to
the local platform's newline style.
=head1 METHODS
=cut
use 5.005;
use strict;
use File::Spec 0.80 ();
use File::Find::Rule 0.20 ();
use File::Slurp 9999.04 ();
use Class::Default 1.0 ();
use FileHandle 0 ();
use Params::Util 0.10 '_INSTANCE';
use vars qw{$VERSION @ISA};
BEGIN {
$VERSION = '1.12';
@ISA = 'Class::Default';
}
#####################################################################
# Constructor and Accessors
=pod
=head2 new param => value, ...
The C<new> constructor creates a new conversion object.
By default, the conversion object will process all files and convert
them to the local platform's newline format.
Takes some optional parameters
=over
=item filter =E<gt> File::Find::Rule
The C<filter> param allows you to provide an instantiate
L<File::Find::Rule> object, that will used to determine the list of
files to check or process.
=item newline =E<gt> $newline
The C<newline> option allows you to provide an alternative newline
format to the local one. The newline format should be provided as a
literal string.
For example, to force Win32 newlines, you would use
my $Object = File::LocalizeNewlines->new( newline => "\015\012" );
=item verbose =E<gt> 1
The C<verbose> option will cause the C<File::LocalizeNewlines> object to
print status information to C<STDOUT> as it runs.
=back
Returns a new C<File::LocalizeNewlines> object.
=cut
sub new {
my $class = ref $_[0] ? ref shift : shift;
my %args = @_;
# Create the basic object
my $self = bless { }, $class;
# Check the file filter
if ( _INSTANCE($args{filter}, 'File::Find::Rule') ) {
$self->{Find} = $args{filter};
$self->{Find}->file->relative;
}
# Allow for a custom platform
$self->{newline} = $args{newline} if $args{newline};
# Check the verbose mode
if ( _CAN($args{verbose}, 'print') ) {
$self->{verbose} = $args{verbose};
} elsif ( $args{verbose} ) {
$self->{verbose} = 1;
}
$self;
}
=pod
=head2 Find
The C<Find> accessor returns the L<File::Find::Rule> object that will be
used for the file search.
=cut
sub Find {
my $self = $_[0]->_self;
$self->{Find} or File::Find::Rule->file->relative;
}
=pod
=head2 newline
The C<newline> accessor returns the newline format that will be used in
the localisation process.
=cut
sub newline {
$_[0]->_self->{newline} or "\n";
}
#####################################################################
# Methods
=pod
=head2 localized $file
The C<localized> method takes an argument of a single file name or
file handle and tests it to see it is localized correctly.
Returns true if localized correctly, false if not, or C<undef> on error.
=cut
sub localized {
my $self = shift->_self;
my $file = (defined $_[0] and ref $_[0]) ? shift
: (defined $_[0] and -f $_[0]) ? shift
: return undef;
my $newline = $self->newline;
my $content = File::Slurp::read_file( $file );
# Create the localized version of the file
my $localized = $content;
$localized =~ s/(?:\015{1,2}\012|\015|\012)/$newline/sg;
$localized eq $content;
}
=pod
=head2 find $dir
The C<find> method takes the path for a dir (or file) and returns a list
of relative files names for all of the files that do B<not> have their
newlines correctly localized.
Returns a list of file names, or the null list if there are no files,
or if an incorrect path was provided.
=cut
sub find {
my $self = shift->_self;
my $path = _DIRECTORY(shift) or return ();
# Find all the files to test
my @files = $self->Find->in( $path );
@files = grep {
! $self->localized(
File::Spec->catfile( $path, $_ )
)
} @files;
@files;
}
=pod
=head2 localize $file | $dir
The C<localize> method takes a file, file handle or directory as argument
and localizes the newlines of the file, or all files within the directory
(that match the filter if one was provided).
Returns the number of files that were localized, zero if no files needed to
be localized, or C<undef> on error.
=cut
sub localize {
my $self = shift->_self;
my $path = (defined $_[0] and ref $_[0]) ? shift
: (defined $_[0] and -e $_[0]) ? shift
: return undef;
# Switch on file or dir
(-f $path or ref $_[0])
? $self->_localize_file( $path )
: $self->_localize_dir( $path );
}
sub _localize_dir {
my $self = shift->_self;
my $path = _DIRECTORY(shift) or return undef;
# Find the files to localise
my @files = $self->Find->in( $path );
# Localize the files
my $count = 0;
my $newline = $self->newline;
foreach ( @files ) {
my $file = File::Spec->catfile( $path, $_ );
my $content = File::Slurp::read_file( $file );
my $localized = $content;
$localized =~ s/(?:\015{1,2}\012|\015|\012)/$newline/sg;
next if $localized eq $content;
File::Slurp::write_file( $file, $localized ) or return undef;
$self->_message( "Localized $file\n" );
$count++;
}
$count;
}
sub _localize_file {
my $self = shift->_self;
my $file = (defined $_[0] and ref $_[0]) ? shift
: (defined $_[0] and -f $_[0]) ? shift
: return undef;
# Does the file need to be localised
my $newline = $self->newline;
my $content = File::Slurp::read_file( $file );
my $localized = $content;
$localized =~ s/(?:\015{1,2}\012|\015|\012)/$newline/sg;
return 0 if $content eq $localized;
# Save the localised version
File::Slurp::write_file( $file, $localized ) or return undef;
$self->_message( "Localized $file\n" ) unless ref $file;
1;
}
sub _message {
my $self = shift;
return 1 unless defined $self->{verbose};
my $message = shift;
$message .= "\n" unless $message =~ /\n$/;
if ( _CAN( $self->{verbose}, 'print' ) ) {
$self->{verbose}->print( $message );
} else {
print STDOUT $message;
}
}
sub _CAN {
(_INSTANCE($_[0], 'UNIVERSAL') and $_[0]->can($_[1])) ? shift : undef;
}
sub _DIRECTORY {
(defined $_[0] and -d $_[0]) ? shift : undef;
}
1;
=pod
=head1 SUPPORT
Bugs should always be submitted via the CPAN bug tracker
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-LocalizeNewlines>
For other issues, contact the maintainer.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 ACKNOWLEDGEMENTS
Thank you to Phase N (L<http://phase-n.com/>) for permitting
the open sourcing and release of this distribution.
L<FileHandle> support added by David Dick E<lt>ddick@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright 2005 - 2009 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|