-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.pl
More file actions
99 lines (84 loc) · 2.1 KB
/
Copy pathcsv.pl
File metadata and controls
99 lines (84 loc) · 2.1 KB
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
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use POSIX qw(ceil);
use FindBin qw($Bin);
use lib qq{$Bin/../lib};
use parent 'Batcher';
sub new {
my $self = shift;
my $vals = shift // {_opts => {}};
my $blsd = bless $vals, $self;
$blsd->_get_opts;
$blsd;
}
sub opts {shift->{'_opts'}}
sub _get_opts {
my $self = shift;
my %get_opts = ();
for my $k (keys %{$self->option_params}) {
my ($long, $short, $type) = $k =~ /^([^|]+)(?:[|]([^=]+))?([=]\w)?$/g;
my $default_val = $self->option_params->{$k};
if (!defined $self->opts->{$long}) {
$self->{'_opts'}->{$long} = $default_val;
}
$get_opts{$k} = \$self->{'_opts'}->{$long};
}
GetOptions(%get_opts);
}
sub option_params {
return {
'debug|D' => 0,
'file|f=s' => undef,
'forks|f=i' => 10,
'limit|l=i' => 0,
'batchsize|s=i' => 2,
};
}
sub csv_file {shift->opts->{'file'}}
sub debug {shift->opts->{'debug'}}
sub forks {shift->opts->{'forks'}}
sub limit {shift->opts->{'limit'}}
sub csv_read {
my $self = shift;
my $header = undef;
my @lines = ();
open my $fh, '<', $self->csv_file or die "Couldn't open csv file!: $!\n";
while (my $line = <$fh>) {
chomp $line;
if (! defined $header) {
$header = $line;
next;
}
push @lines, $line;
}
close $fh;
return @lines;
}
# required by Batcher
sub batch_count {
my $self = shift;
my @lines = $self->csv_read;
my $batch_count = ceil(scalar @lines / $self->batch_size);
return $batch_count;
}
# required by Batcher
sub batch_next {
my ($self, $next_idx) = @_;
my @lines = $self->csv_read;
my $batch_size = $self->batch_size;
my $batch = [splice @lines, $next_idx * $self->batch_size, $batch_size];
return $batch;
}
# required by Batcher
sub batch_size {shift->opts->{'batchsize'}}
# required by Batcher
sub batch_result {
my ($self, $result) = @_;
print "($$) result: $result\n";
# Do something with the result ...
}
__PACKAGE__->new->run;
exit;