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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
package WebService::CIA::Source::DBM;
require 5.005_62;
use strict;
use warnings;
use Fcntl;
use MLDBM qw(DB_File Storable);
use Carp;
use WebService::CIA::Source;
@WebService::CIA::Source::DBM::ISA = ("WebService::CIA::Source");
our $VERSION = '1.4';
sub new {
my $proto = shift;
my $opts = shift;
my $class = ref($proto) || $proto;
my $self = {};
unless (exists $opts->{DBM}) {
croak("WebService::CIA::Source::DBM: No DBM file specified");
}
my $mode;
if (exists $opts->{Mode} && $opts->{Mode} eq "readwrite") {
tie %{$self->{DBM}}, "MLDBM", $opts->{DBM}, O_CREAT|O_RDWR, 0640 or croak "WebService::CIA::Source::DBM: Can't open DBM: $!"; ## no critic (ProhibitLeadingZeros)
} elsif (-e $opts->{DBM}) {
tie %{$self->{DBM}}, "MLDBM", $opts->{DBM}, O_RDONLY, 0440 or croak "WebService::CIA::Source::DBM: Can't open DBM: $!"; ## no critic (ProhibitLeadingZeros)
} else {
croak "WebService::CIA::Source::DBM: $opts->{DBM}: $!";
}
bless ($self, $class);
return $self;
}
sub value {
my $self = shift;
my ($country, $field) = @_;
if (exists $self->dbm->{$country} and exists $self->dbm->{$country}->{$field}) {
return $self->dbm->{$country}->{$field};
} else {
return;
}
}
sub all {
my $self = shift;
my $cc = shift;
if (exists $self->dbm->{$cc}) {
return $self->dbm->{$cc};
} else {
return {};
}
}
sub set {
my $self = shift;
my ($cc, $data) = @_;
$self->dbm->{$cc} = $data;
}
sub dbm {
my $self = shift;
return $self->{DBM};
}
1;
__END__
=head1 NAME
WebService::CIA::Source::DBM - An interface to a DBM copy of the CIA World Factbook
=head1 SYNOPSIS
use WebService::CIA::Source::DBM;
my $source = WebService::CIA::Source::DBM->new({
DBM => 'factbook.dbm',
Mode => 'read'
});
=head1 DESCRIPTION
WebService::CIA::Source::DBM is an interface to a pre-compiled DBM copy of the CIA
World Factbook.
The module can also be used to make the DBM file, taking data from
WebService::CIA::Parser (or WebService::CIA::Source::Web) and inserting it into a DBM.
A script to do this - webservice-cia-makedbm.pl - should be included in this
module's distribution.
=head1 METHODS
Apart from C<new>, these methods are normally accessed via a WebService::CIA object.
=over 4
=item C<new(\%opts)>
This method creates a new WebService::CIA::Source::DBM object. It takes a hashref of
options. Valid keys are "DBM" and "Mode".
DBM is mandatory and should be the location of the DBM file to be used.
Mode is optional and can be either "read" or "readwrite". It defaults to
"read".
=item C<value($country_code, $field)>
Retrieve a value from the DBM.
C<$country_code> should be the FIPS 10-4 country code as defined in
L<https://www.cia.gov/library/publications/the-world-factbook/appendix/appendix-d.html>.
C<$field> should be the name of the field whose value you want to
retrieve, as defined in
L<https://www.cia.gov/library/publications/the-world-factbook/docs/notesanddefs.html>.
(WebService::CIA::Parser also creates four extra fields: "URL", "URL - Print",
"URL - Flag", and "URL - Map" which are the URLs of the country's Factbook
page, the printable version of that page, a GIF map of the country, and a
GIF flag of the country respectively.)
=item C<all($country_code)>
Returns a hashref of field-value pairs for C<$country_code> or an empty
hashref if C<$country_code> isn't in the DBM.
=item C<set($country_code, $data)>
Insert or update data in the DBM.
C<$country_code> should be as described above.
C<$data> is a hashref of the data to store (as Field =E<gt> Value).
C<set> B<overwrites> any data already in the DBM under C<$country_code>.
=item C<dbm()>
Returns a reference to the DBM file in use.
=back
=head1 AUTHOR
Ian Malpass (ian-cpan@indecorous.com)
=head1 COPYRIGHT
Copyright 2003-2007, Ian Malpass
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
The CIA World Factbook's copyright information page
(L<https://www.cia.gov/library/publications/the-world-factbook/docs/contributor_copyright.html>)
states:
The Factbook is in the public domain. Accordingly, it may be copied
freely without permission of the Central Intelligence Agency (CIA).
=head1 SEE ALSO
WebService::CIA, WebService::CIA::Parser, WebService::CIA::Source::Web
=cut
|