| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl | 
| 2 |  |  |  |  |  |  | package Geo::CountryFlags; | 
| 3 | 1 |  |  | 1 |  | 198627 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 35 |  | 
| 4 | 1 |  |  | 1 |  | 622 | use Geo::CountryFlags::I2C; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 5 | 1 |  |  | 1 |  | 6 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 336 |  | 
| 6 |  |  |  |  |  |  | $VERSION = do { my @r = (q$Revision: 1.01 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | my $i2c = subref Geo::CountryFlags::I2C; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =head1 NAME | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | Geo::CountryFlags - dynamically fetch flag gif's from CIA | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | use Geo::CountryFlags | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | $gcf = new Geo::CountryFlags; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | return a local path to the flag file | 
| 21 |  |  |  |  |  |  | fetch the file from CIA if necessary | 
| 22 |  |  |  |  |  |  | and put it in the flag directory | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | $flag_path = $gcf->get_flag($country_code,[flag_dir]) | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | default: | 
| 27 |  |  |  |  |  |  | flag_dir = ./flags | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | retrieve the CIA country code | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | $cia_code	= $gcf->cc2cia($country_code) | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | retrieve the ISO country name | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | $gci = new Geo::CountryFlags::ISO; | 
| 36 |  |  |  |  |  |  | $country_name	= $gci->value($country_code); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | retrieve the CIA country name | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | $gcc = new Geo::CountryFlags::CIA; | 
| 41 |  |  |  |  |  |  | $country_name = $gcc->value($cia_code); | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | Provides methods to display / retrieve flag gifs dynamically from the web | 
| 46 |  |  |  |  |  |  | site of the Central Intelligence Agency. Permanently caches a | 
| 47 |  |  |  |  |  |  | local copy of the flag gif in your web site sub directory. | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | The flags for all country codes as of module publication are included | 
| 50 |  |  |  |  |  |  | in the ./flags directory should you wish to install them. However, | 
| 51 |  |  |  |  |  |  | If LWP::Simple is installed, Geo::CountryFlags will fetch them as needed | 
| 52 |  |  |  |  |  |  | and store them in ./flags [default] or the directory of you choice on your | 
| 53 |  |  |  |  |  |  | web site. | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | To fetch a single flag PATH the usage is simply: | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | my $cc = 'US';	# country code | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | my $flag_path = Geo::CountryFlags->new->get_flag($cc); | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | for multiple flags: | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | $gcf = new Geo::CountryFlags; | 
| 64 |  |  |  |  |  |  | for (blah.... blah) { | 
| 65 |  |  |  |  |  |  | my $cc = function_of(blah...); | 
| 66 |  |  |  |  |  |  | my $flag_path = $gcf->get_flag($cc); | 
| 67 |  |  |  |  |  |  | .... | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =head1 METHODS | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | =over 4 | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =item $gcf = new Geo::CountryFlags; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | input:	none | 
| 77 |  |  |  |  |  |  | returns:	blessed package reference | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =cut | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub new { | 
| 82 | 1 |  |  | 1 | 1 | 457 | my $proto = shift; | 
| 83 | 1 |  | 33 |  |  | 9 | my $class = ref($proto) || $proto; | 
| 84 | 1 |  |  |  |  | 3 | my $self  = {}; | 
| 85 | 1 |  |  |  |  | 5 | bless $self, $class; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =item $flag_path=$gf->get_flag($country_code,[flag_dir]); | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | input:	country code, | 
| 91 |  |  |  |  |  |  | flag directory (optional) | 
| 92 |  |  |  |  |  |  | default = ./flags | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | output:	path_to/flag.image | 
| 95 |  |  |  |  |  |  | or undef if the country | 
| 96 |  |  |  |  |  |  | flag is not available | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | $@	:	clear on normal return | 
| 99 |  |  |  |  |  |  | set to error if unable to | 
| 100 |  |  |  |  |  |  | connect or retrieve file | 
| 101 |  |  |  |  |  |  | from target flag server | 
| 102 |  |  |  |  |  |  | (only set on undef return) | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =cut | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | my $gcu; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub get_flag { | 
| 109 | 3 |  |  | 3 | 1 | 632 | my ($self,$cc,$fd) = @_; | 
| 110 | 3 | 50 |  |  |  | 8 | return undef unless $cc; | 
| 111 | 3 | 100 |  |  |  | 11 | $fd = './flags' unless $fd; | 
| 112 | 3 | 100 |  |  |  | 37 | unless ( -e $fd) { | 
| 113 | 1 | 50 |  |  |  | 8 | if (-d $fd) { | 
| 114 | 0 |  |  |  |  | 0 | eval {die "$fd is not a directory"}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 115 | 0 |  |  |  |  | 0 | return undef; | 
| 116 |  |  |  |  |  |  | } else { | 
| 117 | 1 |  |  |  |  | 134 | mkdir $fd, 0755; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | } | 
| 120 | 3 |  |  |  |  | 6 | undef $@; | 
| 121 | 3 |  |  |  |  | 8 | my $fp = $fd .'/'. $cc .'-flag.gif'; | 
| 122 | 3 | 100 |  |  |  | 57 | return $fp if -e $fp;			# return flag if it exists | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 2 | 100 |  |  |  | 7 | my $cia = $i2c->($cc) or return undef; | 
| 125 | 1 |  |  |  |  | 830 | require LWP::Simple; | 
| 126 | 1 | 50 |  |  |  | 664016 | unless ($gcu) { | 
| 127 | 1 |  |  |  |  | 1542 | require Geo::CountryFlags::URLs; | 
| 128 | 1 |  |  |  |  | 10 | $gcu = new Geo::CountryFlags::URLs; | 
| 129 |  |  |  |  |  |  | } | 
| 130 | 1 | 50 |  |  |  | 3 | return undef unless eval {		# response must be 200, OK | 
| 131 | 1 | 50 |  |  |  | 15 | 200 == ($_ = &LWP::Simple::getstore( | 
| 132 |  |  |  |  |  |  | $gcu->CIAFLAGS . $cia .'-flag.gif', | 
| 133 |  |  |  |  |  |  | $fp)) || | 
| 134 |  |  |  |  |  |  | die $_ | 
| 135 |  |  |  |  |  |  | }; | 
| 136 | 0 |  |  |  |  | 0 | return $fp; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =item $cia_code=$gf->cc2cia($country_code); | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | input:	country code | 
| 142 |  |  |  |  |  |  | output:	cia code | 
| 143 |  |  |  |  |  |  | or | 
| 144 |  |  |  |  |  |  | undef is cia code | 
| 145 |  |  |  |  |  |  | is known absent | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =cut | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | sub cc2cia { | 
| 150 | 3 |  |  | 3 | 1 | 308 | shift; | 
| 151 | 3 |  |  |  |  | 13 | goto &$i2c; | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =pod | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =back | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =cut | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | 1; | 
| 161 |  |  |  |  |  |  | __END__ |