File: 2.pl

package info (click to toggle)
libfile-slurp-tiny-perl 0.004-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 156 kB
  • sloc: perl: 166; makefile: 10
file content (39 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use Carp 'croak';
use Benchmark ':hireswallclock', 'cmpthese';
#use File::Slurp 'read_file';

sub read1 {
	my ($filename, $encoding, %options) = @_;
	$encoding |= 'utf-8';
	my $extra = $options{crlf} ? ':crlf' : '';

	open my $fh, "<$extra:encoding($encoding)", $filename or croak "Couldn't open $filename: $!";
	my $size = -s $fh;
	my ($pos, $read, $buf) = 0;
	do {
		defined($read = read $fh, $buf, $size - $pos, $pos) or croak "Couldn't read $filename: $!";
		$pos += $read;
	} while ($read && $pos < $size);
}

sub read2 {
	my ($filename, $encoding, %options) = @_;
	$encoding |= 'utf-8';
	my $extra = $options{crlf} ? ':crlf' : '';

	open my $fh, "<$extra:encoding($encoding)", $filename or croak "Couldn't open $filename: $!";
	my $buf_ref = do { local $/; <$fh> };
}

my $filename = shift or die "No argument given";
my $count = shift || 100;
my $factor = 1;

print "Slurping utf8\n";
cmpthese($count * $factor, {
#	slurp => sub { read_file($filename, binmode => ':encoding(utf-8)') },
	smart => sub { read1($filename) },
	dump  => sub { read2($filename) },
});