Skip to content

Commit

Permalink
Make read_lines arguments positional too
Browse files Browse the repository at this point in the history
  • Loading branch information
Leont committed Apr 18, 2015
1 parent e0d5e3c commit 9850f1a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
20 changes: 6 additions & 14 deletions lib/File/Slurper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ sub write_binary {
}

sub read_lines {
my ($filename, $encoding, %options) = @_;
my ($filename, $encoding, $crlf, $skip_chomp) = @_;
$encoding ||= 'utf-8';
my $layer = _text_layers($encoding, $options{crlf});
my $layer = _text_layers($encoding, $crlf);

open my $fh, "<$layer", $filename or croak "Couldn't open $filename: $!";
return <$fh> if not %options;
return <$fh> if $skip_chomp;
my @buf = <$fh>;
close $fh;
chomp @buf if $options{chomp};
chomp @buf;
return @buf;
}

Expand Down Expand Up @@ -114,17 +114,9 @@ Reads file C<$filename> into a scalar and decodes it from C<$encoding> (which de
Reads file C<$filename> into a scalar without any decoding or transformation.
=func read_lines($filename, $encoding, %options)
Reads file C<$filename> into a list/array after decoding from C<$encoding>. By default it returns this list. Can optionally take this named argument:
=over 4
=func read_lines($filename, $encoding, $crlf, $skip_chomp)
=item * chomp
C<chomp> the lines.
=back
Reads file C<$filename> into a list/array line-by-line, after decoding from C<$encoding>, optional crlf translation and chomping.
=func write_text($filename, $content, $encoding, $crlf)
Expand Down
5 changes: 3 additions & 2 deletions t/10-basics.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ is(read_binary($0), $content, 'read_binary() works');

my @content = split /(?<=\n)/, $content;

is_deeply([ read_lines($0) ], \@content, 'read_lines returns the right thing');
is_deeply([ read_lines($0, 'utf-8', 0, 1) ], \@content, 'read_lines returns the right thing (no chomp)');
chomp @content;
is_deeply([ read_lines($0, 'utf-8', chomp => 1) ], \@content, 'read_lines(chomp => 1) returns the right thing');

is_deeply([ read_lines($0) ], \@content, 'read_lines returns the right thing (chomp)');

is_deeply([ read_dir('lib') ], [ 'File' ], 'read_dir appears to work');

Expand Down

0 comments on commit 9850f1a

Please sign in to comment.