line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/env perl |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
27718
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Gwybodaeth::Read; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Methods for reading in data from either a file or URL; |
10
|
1
|
|
|
1
|
|
1068
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
110441
|
|
|
1
|
|
|
|
|
30
|
|
11
|
1
|
|
|
1
|
|
10
|
use HTTP::Request; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
12
|
1
|
|
|
1
|
|
6
|
use Carp qw(croak); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
696
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Read - input data reader class for gwybodaeth |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Read; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $r = Read->new(); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$r->get_file_data("/home/foo/bar.csv"); |
25
|
|
|
|
|
|
|
$r->get_url_data("www.example.org/bar.csv"); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$r->get_input_data(); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
This module imports data from the URIs given to it. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item new() |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Create a new instance of Read. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$r = Read->new(); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=cut |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub new { |
45
|
1
|
|
|
1
|
1
|
75
|
my $class = shift; |
46
|
1
|
|
|
|
|
6
|
my $self = {'Data' => [] }; |
47
|
1
|
|
|
|
|
3
|
bless $self, $class; |
48
|
1
|
|
|
|
|
4
|
return $self; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item get_file_data($filename) |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
This function gets data from $filename. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
# Open a file and store its contents |
58
|
|
|
|
|
|
|
# Returns length of file data |
59
|
|
|
|
|
|
|
sub get_file_data { |
60
|
2
|
50
|
|
2
|
1
|
837
|
ref(my $self = shift) or croak "instance variable needed"; |
61
|
2
|
|
|
|
|
7
|
my $file = shift; |
62
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
3
|
my $fh; |
64
|
|
|
|
|
|
|
|
65
|
2
|
50
|
|
|
|
19
|
if (fileno($file)) { # Check if its a file handle |
66
|
0
|
|
|
|
|
0
|
$fh = $file; |
67
|
|
|
|
|
|
|
} else { # If it's just a file name open a file handle |
68
|
|
|
|
|
|
|
# Return if file doesn't exist |
69
|
2
|
100
|
|
|
|
42
|
unless ( -e $file ) { return 0 }; |
|
1
|
|
|
|
|
6
|
|
70
|
|
|
|
|
|
|
|
71
|
1
|
50
|
|
|
|
50
|
open $fh, q{<}, $file or croak "Couldn't open $file: $!"; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
54
|
@{ $self->{Data} }= (<$fh>); |
|
1
|
|
|
|
|
14
|
|
75
|
|
|
|
|
|
|
|
76
|
1
|
|
|
|
|
14
|
close $fh; |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
10
|
return int $self->{Data}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item get_url_data($url) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
This function gets data from $url. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Open a URL download the body and store it |
88
|
|
|
|
|
|
|
# Returns true if successful |
89
|
|
|
|
|
|
|
sub get_url_data { |
90
|
2
|
|
|
2
|
1
|
249821
|
my($self, $url) = @_; |
91
|
|
|
|
|
|
|
|
92
|
2
|
50
|
|
|
|
12
|
ref($self) or croak "instance variable needed"; |
93
|
|
|
|
|
|
|
|
94
|
2
|
|
|
|
|
16
|
my $browser = LWP::UserAgent->new(); |
95
|
2
|
|
|
|
|
679
|
my $req = HTTP::Request->new(GET => $url); |
96
|
2
|
|
|
|
|
483
|
my $res = $browser->get($url); |
97
|
|
|
|
|
|
|
|
98
|
2
|
100
|
|
|
|
149791
|
if ($res->is_success) { |
99
|
1
|
|
|
|
|
30
|
@{ $self->{Data} } = split / |
|
1
|
|
|
|
|
4117
|
|
100
|
|
|
|
|
|
|
\n\r? # new line feed and possible carriage return |
101
|
|
|
|
|
|
|
| # OR |
102
|
|
|
|
|
|
|
\r\n? # carriage return and possible new line feed |
103
|
|
|
|
|
|
|
/x, $res->decoded_content; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
2
|
|
|
|
|
19
|
return $res->is_success; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item get_input_data() |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This function returns an array contiaining the ingested data. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=cut |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# Data return methods: |
116
|
|
|
|
|
|
|
sub get_input_data { |
117
|
1
|
50
|
|
1
|
1
|
6259
|
ref(my $self = shift) or croak "instance variable needed"; |
118
|
1
|
|
|
|
|
7
|
return $self->{Data}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
__END__ |