Finance-Currency-Convert

 view release on metacpan or  search on metacpan

Convert.pm  view on Meta::CPAN


	open(RATES, "<", $self->{RatesFile}) or ( warn("Can't read $self->{RatesFile}\n") and return );
	while(local $_ = <RATES>) {
		my ($source, $targetrates) = split(/\|/, $_);
		foreach my $target (split(/\:/, $targetrates)) {
			my @pieces = split(/\=/, $target);
			if (scalar(@pieces) > 1) {
				$self->setRate($source, $pieces[0], $pieces[1]);
			}
		}
	}
	close(RATES);
}

sub writeRatesFile() {
	my $self = shift;
	return if (!defined $self->{RatesFile});

	open(RATES, ">", $self->{RatesFile}) or ( warn("Can't access $self->{RatesFile}") and return );
	foreach my $sourcerate (sort keys %{$self->{CurrencyRates}}) {
		print RATES "$sourcerate|";
		foreach my $targetrate (sort keys %{ $self->{CurrencyRates}{$sourcerate}}) {
			if (exists($self->{CurrencyRates}{$sourcerate}{$targetrate})
				&& defined($self->{CurrencyRates}{$sourcerate}{$targetrate})
				&& $self->{CurrencyRates}{$sourcerate}{$targetrate} ne '') {
				print RATES "$targetrate=" . $self->{CurrencyRates}{$sourcerate}{$targetrate} . ":";
			}
		}
		print RATES "\n";
	}
	close(RATES);
}

sub _getQuoteFetcher() {
	my $self = shift;
	# test if Finance::Quote is available
	eval { require Finance::Quote; };
	if ($@) {
		warn "Finance::Quote not installed - can't use updateRates()\n";
		return undef;
	};
	# test if Finance::Quote::CurrencyRates::ECB is available
	my $ecbAvailable = 0;
	eval { require Finance::Quote::CurrencyRates::ECB; };
	if ($@) {
		warn "Finance::Quote::CurrencyRates::ECB not installed\n";
	} else {
		$ecbAvailable = 1;
	};
	# get the exchange rates
	my $q;
	if ($ecbAvailable) {
		$q = Finance::Quote->new(currency_rates => {order => ['ECB']});
	} else {
		$q = Finance::Quote->new();
	}
	$q->user_agent->agent($self->{UserAgent});
	return $q;
}

sub updateRates() {
	my $self = shift;
	my @CurrencyList = @_;
	my $q = $self->_getQuoteFetcher();
	return if (!defined($q));
	foreach my $source (@CurrencyList) {
		foreach my $target (sort keys %{ $self->{CurrencyRates}}) {
			$self->setRate($source, $target, $q->currency($source, $target));
		}
	}
	foreach my $source (sort keys %{ $self->{CurrencyRates}}) {
		foreach my $target (@CurrencyList) {
			$self->setRate($source, $target, $q->currency($source, $target));
		}
	}
}

sub updateRate() {
	my $self = shift;
	my $source = shift;
	my $target = shift;
	my $q = $self->_getQuoteFetcher();
	return if (!defined($q));
	# get the exchange rate
	$self->setRate($source, $target, $q->currency($source, $target));
}

sub setUserAgent() {
	my $self = shift;
	$self->{UserAgent} = shift;
}

sub rateAvailable {
	my $self = shift;
	my $source = shift;
	my $target = shift;
	return defined($self->{CurrencyRates}->{$source}{$target});
}

sub convert() {
	my $self = shift;
	my $amount = shift;
	my $source = shift;
	my $target = shift;
	return $amount * $self->{CurrencyRates}->{$source}{$target};
}

sub convertFromEUR() {
	my $self = shift;
	my $amount = shift;
	my $target = shift;
	return $self->convert($amount, "EUR", $target);
}

sub convertToEUR() {
	my $self = shift;
	my $amount = shift;
	my $source = shift;
	return $self->convert($amount, $source, "EUR");
}

1;

__END__

=pod

=head1 NAME

Finance::Currency::Convert - Convert currencies and fetch their exchange rates

=head1 SYNOPSIS

   use Finance::Currency::Convert;
   my $converter = new Finance::Currency::Convert;

   $amount_euro = $converter->convert(100, "DEM", "EUR");
   $amount_euro = $converter->convertToEUR(100, "DEM");



( run in 0.565 second using v1.01-cache-2.11-cpan-13bb782fe5a )