| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | # | ||||||||||
| 4 | # Copyright (C) 2018 Joelle Maslak | ||||||||||
| 5 | # All Rights Reserved - See License | ||||||||||
| 6 | # | ||||||||||
| 7 | |||||||||||
| 8 | package File::ByLine; | ||||||||||
| 9 | 79 | $File::ByLine::VERSION = '1.192590'; | |||||||||
| 10 | 79 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | # ABSTRACT: Line-by-line file access loops | ||||||||||
| 13 | |||||||||||
| 14 | 79 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 79 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 79 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 79 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 79 | ▶ | use Fcntl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 79 | ▶ | use File::ByLine::Object; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 79 | ▶ | use Scalar::Util qw(reftype); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | # Object with default options | ||||||||||
| 24 | 79 | our $OBJ = File::ByLine::Object->new(); | |||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | # | ||||||||||
| 28 | # Exports | ||||||||||
| 29 | # | ||||||||||
| 30 | 79 | require Exporter; | |||||||||
| 31 | 79 | our @ISA = qw(Exporter); | |||||||||
| 32 | |||||||||||
| 33 | ## no critic (Modules::ProhibitAutomaticExportation) | ||||||||||
| 34 | 79 | our @EXPORT = | |||||||||
| 35 | qw(dolines forlines greplines maplines parallel_dolines parallel_forlines parallel_greplines parallel_maplines readlines writefile appendfile); | ||||||||||
| 36 | ## use critic | ||||||||||
| 37 | |||||||||||
| 38 | 79 | our @EXPORT_OK = | |||||||||
| 39 | qw(dolines forlines greplines maplines parallel_dolines parallel_forlines parallel_greplines parallel_maplines readlines writefile appendfile); | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | 3 | ▶ | sub dolines (&$) { | ||||||||
|
Subroutine
dolines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 3 | my ( $code, $file ) = @_; | |||||||||
| 44 | |||||||||||
| 45 | 3 | return $OBJ->do( $code, $file ); | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 2 | ▶ | sub forlines ($&) { | ||||||||
|
Subroutine
forlines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 2 | my ( $file, $code ) = @_; | |||||||||
| 51 | |||||||||||
| 52 | 2 | return $OBJ->do( $code, $file ); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | 323 | ▶ | sub parallel_dolines (&$$) { | ||||||||
|
Subroutine
parallel_dolines: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 57 | 323 | my ( $code, $file, $procs ) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 323 | ▶ | if ( !defined($procs) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 61 | croak("Must include number of child processes"); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 262 | my $byline = File::ByLine::Object->new(); | |||||||||
| 66 | 262 | $byline->processes($procs); | |||||||||
| 67 | |||||||||||
| 68 | 140 | return $byline->do( $code, $file ); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | |||||||||||
| 72 | 184 | ▶ | sub parallel_forlines ($$&) { | ||||||||
|
Subroutine
parallel_forlines: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 73 | 184 | my ( $file, $procs, $code ) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 184 | ▶ | if ( !defined($procs) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 61 | croak("Must include number of child processes"); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | |||||||||||
| 81 | 123 | my $byline = File::ByLine::Object->new(); | |||||||||
| 82 | 123 | $byline->processes($procs); | |||||||||
| 83 | |||||||||||
| 84 | 123 | return $byline->do( $code, $file ); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | sub greplines (&$) { | ||||||||
|
Subroutine
greplines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | 1 | my ( $code, $file ) = @_; | |||||||||
| 90 | |||||||||||
| 91 | 1 | return $OBJ->grep( $code, $file ); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | 130 | ▶ | sub parallel_greplines (&$$) { | ||||||||
|
Subroutine
parallel_greplines: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 96 | 130 | my ( $code, $file, $procs ) = @_; | |||||||||
| 97 | |||||||||||
| 98 | 130 | ▶ | if ( !defined($procs) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 61 | croak("Must include number of child processes"); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | ▶ | if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | 69 | my $byline = File::ByLine::Object->new(); | |||||||||
| 105 | 69 | $byline->processes($procs); | |||||||||
| 106 | |||||||||||
| 107 | 69 | return $byline->grep( $code, $file ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | |||||||||||
| 111 | 2 | ▶ | sub maplines (&$) { | ||||||||
|
Subroutine
maplines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 112 | 2 | my ( $code, $file ) = @_; | |||||||||
| 113 | |||||||||||
| 114 | 2 | return $OBJ->map( $code, $file ); | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | 170 | ▶ | sub parallel_maplines (&$$) { | ||||||||
|
Subroutine
parallel_maplines: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||
| 119 | 170 | my ( $code, $file, $procs ) = @_; | |||||||||
| 120 | |||||||||||
| 121 | 170 | ▶ | if ( !defined($procs) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 61 | croak("Must include number of child processes"); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | if ( $procs <= 0 ) { croak("Number of processes must be >= 1"); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | |||||||||||
| 127 | 109 | my $byline = File::ByLine::Object->new(); | |||||||||
| 128 | 109 | $byline->processes($procs); | |||||||||
| 129 | |||||||||||
| 130 | 109 | return $byline->map( $code, $file ); | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | |||||||||||
| 134 | 14 | ▶ | sub readlines ($) { | ||||||||
|
Subroutine
readlines: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 135 | 14 | my ($file) = @_; | |||||||||
| 136 | |||||||||||
| 137 | 14 | return $OBJ->lines($file); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | 3 | ▶ | sub writefile ($@) { | ||||||||
|
Subroutine
writefile: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||
| 142 | 3 | my ( $file, @lines ) = @_; | |||||||||
| 143 | 0 | ▶ | if ( !defined($file) ) { die("Must define the filename"); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | |||||||||||
| 145 | # Last line should have it's newline removed, if applicable | ||||||||||
| 146 | 3 | ▶ | if (@lines) { $lines[-1] =~ s/\n$//s; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 147 | |||||||||||
| 148 | 3 | open my $fh, '>', $file; | |||||||||
| 149 | 3 | foreach my $line (@lines) { | |||||||||
| 150 | 7 | say $fh $line; | |||||||||
| 151 | } | ||||||||||
| 152 | 3 | close $fh; | |||||||||
| 153 | |||||||||||
| 154 | 3 | return; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | 4 | ▶ | sub appendfile ($@) { | ||||||||
|
Subroutine
appendfile: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||
| 159 | 4 | my ( $file, @lines ) = @_; | |||||||||
| 160 | 0 | ▶ | if ( !defined($file) ) { die("Must define the filename"); } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | |||||||||||
| 162 | # Last line should have it's newline removed, if applicable | ||||||||||
| 163 | 4 | ▶ | if (@lines) { $lines[-1] =~ s/\n$//s; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | |||||||||||
| 165 | 4 | open my $fh, '>>', $file; | |||||||||
| 166 | 4 | foreach my $line (@lines) { | |||||||||
| 167 | 10 | say $fh $line; | |||||||||
| 168 | } | ||||||||||
| 169 | 4 | close $fh; | |||||||||
| 170 | |||||||||||
| 171 | 4 | return; | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | # | ||||||||||
| 175 | # Object Oriented Interface | ||||||||||
| 176 | # | ||||||||||
| 177 | |||||||||||
| 178 | 172 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 179 | 172 | shift; # Remove the first parameter because we want to specify the class. | |||||||||
| 180 | |||||||||||
| 181 | 172 | return File::ByLine::Object->new(@_); | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | |||||||||||
| 185 | 79 | 1; | |||||||||
| 186 | |||||||||||
| 187 | __END__ | ||||||||||