#!/usr/bin/perl -w
use strict;

# quote.pl v. 1.0
# 2008 - Seth Just
# Licensed under Creative Commons Attribution-Noncommercial-Share Alike 3.0 License
# This software is provided with no warranty of any kind.
# The author is not liable for any consequences arising from the use of this software.

# import module
use Finance::Quote;

# create object
my $q = Finance::Quote->new();

#print usage information
if (length(@ARGV) == 0) {
	print "Usage: $0 ticker1, ticker2, ... , tickerN\n";
	exit;
}

# get stock symbols from the command line and
# format them correctly (uppercase)
for (@ARGV){
	$_ = uc();
}

# retrieve stock quote
my %data = $q->fetch('usa', @ARGV);

# print result for each stock
for (@ARGV){
	if ($data{$_, 'success'}) {			# if getting the quote succeeded
		my $name = $data{$_, 'name'};	# build a report
		my $price = $data{$_, 'price'};
		my $message = '';
		$message .= $name . ' (' . $_ . ')';
		$message .= ' ' x(25 - length($message));
		$message .= "\$$price\n";
		print $message;
	}
	else { print "Failed to retrieve quote for $_: $data{$_, 'errormsg'}\n"; }
}
