line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Parse::SAMGov; |
2
|
|
|
|
|
|
|
$Parse::SAMGov::VERSION = '0.106'; |
3
|
2
|
|
|
2
|
|
1038
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
4
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
49
|
|
5
|
2
|
|
|
2
|
|
47
|
use 5.010; |
|
2
|
|
|
|
|
4
|
|
6
|
2
|
|
|
2
|
|
6
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
101
|
|
7
|
2
|
|
|
2
|
|
451
|
use IO::All; |
|
2
|
|
|
|
|
8454
|
|
|
2
|
|
|
|
|
18
|
|
8
|
2
|
|
|
2
|
|
1581
|
use Text::CSV_XS; |
|
2
|
|
|
|
|
13952
|
|
|
2
|
|
|
|
|
103
|
|
9
|
2
|
|
|
2
|
|
860
|
use Parse::SAMGov::Entity; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
62
|
|
10
|
2
|
|
|
2
|
|
941
|
use Parse::SAMGov::Exclusion; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
47
|
|
11
|
2
|
|
|
2
|
|
10
|
use Parse::SAMGov::Mo; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
6
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ABSTRACT: Parses SAM Entity Management Public Extract Layout from SAM.gov |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub parse_file { |
17
|
3
|
|
|
3
|
1
|
17002
|
my ($self, $filename, $cb, $cb_arg) = @_; |
18
|
3
|
50
|
|
|
|
88
|
croak "Unable to open file $filename: $!" unless -e $filename; |
19
|
3
|
|
|
|
|
17
|
my $io = io $filename; |
20
|
3
|
50
|
|
|
|
473
|
croak "Unable to create IO::All object for reading $filename" |
21
|
|
|
|
|
|
|
unless defined $io; |
22
|
3
|
|
|
|
|
7
|
my $result = []; |
23
|
3
|
|
|
|
|
6
|
my $is_entity = 0; |
24
|
3
|
|
|
|
|
6
|
my $entity_info = {}; |
25
|
3
|
|
|
|
|
19
|
while (my $line = $io->getline) { |
26
|
17
|
|
|
|
|
2349
|
chomp $line; |
27
|
17
|
|
|
|
|
35
|
$line =~ s/^\s+//g; |
28
|
17
|
|
|
|
|
99
|
$line =~ s/\s+$//g; |
29
|
17
|
50
|
|
|
|
35
|
next unless length $line; |
30
|
17
|
|
|
|
|
74
|
my $obj = Parse::SAMGov::Entity->new; |
31
|
17
|
100
|
|
|
|
89
|
if ($line =~ /BOF PUBLIC\s+(\d{8})\s+(\d{8})\s+(\d+)\s+(\d+)/) { |
|
|
100
|
|
|
|
|
|
32
|
2
|
|
|
|
|
3
|
$is_entity = 1; |
33
|
2
|
|
|
|
|
8
|
$entity_info->{date} = $1; |
34
|
2
|
|
|
|
|
6
|
$entity_info->{rows} = $3; |
35
|
2
|
|
|
|
|
7
|
$entity_info->{seqno} = $4; |
36
|
2
|
|
|
|
|
20
|
next; |
37
|
|
|
|
|
|
|
} elsif ($line =~ /EOF\s+PUBLIC\s+(\d{8})\s+(\d{8})\s+(\d+)\s+(\d+)/) { |
38
|
|
|
|
|
|
|
croak "Invalid footer q{$line} in file" |
39
|
|
|
|
|
|
|
if ( $entity_info->{date} ne $1 |
40
|
|
|
|
|
|
|
or $entity_info->{rows} ne $3 |
41
|
2
|
50
|
33
|
|
|
25
|
or $entity_info->{seqno} ne $4); |
|
|
|
33
|
|
|
|
|
42
|
2
|
|
|
|
|
7
|
last; |
43
|
|
|
|
|
|
|
} else { |
44
|
13
|
100
|
|
|
|
29
|
last unless $is_entity; # skip this loop and do something else |
45
|
12
|
|
|
|
|
253
|
my @data = split /\|/x, $line; |
46
|
12
|
50
|
|
|
|
50
|
carp "Invalid data line \n$line\n" unless $obj->load(@data); |
47
|
|
|
|
|
|
|
} |
48
|
12
|
100
|
66
|
|
|
48
|
if (defined $cb and ref $cb eq 'CODE') { |
49
|
6
|
|
|
|
|
14
|
my $res = &$cb($obj, $cb_arg); |
50
|
6
|
100
|
|
|
|
150
|
push @$result, $obj if $res; |
51
|
|
|
|
|
|
|
} else { |
52
|
6
|
|
|
|
|
44
|
push @$result, $obj; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
3
|
100
|
|
|
|
6
|
unless ($is_entity) { |
56
|
1
|
50
|
|
|
|
11
|
my $csv = Text::CSV_XS->new({ binary => 1 }) |
57
|
|
|
|
|
|
|
or croak "Failed to create Text::CSV_XS object: " |
58
|
|
|
|
|
|
|
. Text::CSV_XS->error_diag(); |
59
|
1
|
|
|
|
|
119
|
my $obj = Parse::SAMGov::Exclusion->new; |
60
|
1
|
|
|
1
|
|
3
|
while (my $row = $csv->getline($io->io_handle)) { |
|
1
|
|
|
|
|
58
|
|
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
61
|
4
|
50
|
|
|
|
264
|
carp "Invalid data line \n$row\n" unless $obj->load(@$row); |
62
|
4
|
50
|
33
|
|
|
10
|
if (defined $cb and ref $cb eq 'CODE') { |
63
|
0
|
|
|
|
|
0
|
my $res = &$cb($obj, $cb_arg); |
64
|
0
|
0
|
|
|
|
0
|
push @$result, $obj if $res; |
65
|
|
|
|
|
|
|
} else { |
66
|
4
|
|
|
|
|
34
|
push @$result, $obj; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
1
|
50
|
|
|
|
101
|
$csv->eof or $csv->error_diag(); |
70
|
|
|
|
|
|
|
} |
71
|
3
|
50
|
|
|
|
47
|
return $result if scalar @$result; |
72
|
0
|
|
|
|
|
0
|
return; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=pod |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=encoding UTF-8 |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 NAME |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Parse::SAMGov - Parses SAM Entity Management Public Extract Layout from SAM.gov |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 VERSION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
version 0.106 |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 SYNOPSIS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $parser = Parse::SAMGov->new; |
92
|
|
|
|
|
|
|
my $entities = $parser->parse_file('SAM_PUBLIC_DAILY_20160701.dat'); |
93
|
|
|
|
|
|
|
foreach my $e (@$entities) { |
94
|
|
|
|
|
|
|
## do something with each entity |
95
|
|
|
|
|
|
|
say $e->DUNS, ' is a valid entity'; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
#... use in filter mode like grep ... |
98
|
|
|
|
|
|
|
my $entities_541511 = $parser->parse_file('SAM_PUBLIC_DAILY_20160701.dat', |
99
|
|
|
|
|
|
|
sub { |
100
|
|
|
|
|
|
|
# filter all companies with NAICS code |
101
|
|
|
|
|
|
|
# being 541511 |
102
|
|
|
|
|
|
|
return $_[0] if exists $_[0]->NAICS->{541511}; |
103
|
|
|
|
|
|
|
return undef; |
104
|
|
|
|
|
|
|
}); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# ... do something ... |
107
|
|
|
|
|
|
|
my $exclusions = $parser->parse_file(exclusion => 'SAM_Exclusions_Public_Extract_16202.CSV'); |
108
|
|
|
|
|
|
|
foreach my $e (@$exclusions) { |
109
|
|
|
|
|
|
|
## do something with each entity that has been excluded |
110
|
|
|
|
|
|
|
say $e->DUNS, ' has been excluded'; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 METHODS |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 parse_file |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
This method takes as arguments the file to be parsed and returns an array |
118
|
|
|
|
|
|
|
reference of L or L objects |
119
|
|
|
|
|
|
|
depending on the data being parsed. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
If the second argument is a coderef then passes each Entity or |
122
|
|
|
|
|
|
|
Exclusion object into the callback where the user can select which objects they |
123
|
|
|
|
|
|
|
want to return. The user has to return 1 if they want the object returned in the |
124
|
|
|
|
|
|
|
array ref or undef if they do not. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $entities = $parser->parse_file('SAM_PUBLIC_DAILY_20160701.dat'); |
127
|
|
|
|
|
|
|
my $exclusions = $parser->parse_file('SAM_Exclusions_Public_Extract_16202.CSV'); |
128
|
|
|
|
|
|
|
my $entities = $parser->parse_file('SAM_PUBLIC_DAILY_20160701.dat', sub { |
129
|
|
|
|
|
|
|
my ($entity_or_exclusion, $optional_user_arg) = @_; |
130
|
|
|
|
|
|
|
#... do something ... |
131
|
|
|
|
|
|
|
return 1 if (!$entity_or_exclusion->is_private); |
132
|
|
|
|
|
|
|
return undef; |
133
|
|
|
|
|
|
|
}, $optional_user_arg); |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 SEE ALSO |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
L and L for the object |
138
|
|
|
|
|
|
|
definitions. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 AUTHOR |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Vikas N Kumar |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This software is copyright (c) 2016 by Selective Intellect LLC. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
149
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=cut |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
__END__ |