This simple example uses Finance::Quote Perl module to fetch real time stock quotes for a companies listed in a CSV file. First we need to install Finance::Quote perl module: Feel free to get from a cpan or use package management tool to get from your linux distribution's repository. For debian o ubuntu do:

# apt-get install libfinance-quote-perl

Here is our sample CSV file containing list of ticker and exchange values:

stocks.csv:

AAON,NASDAQ
CMS,NYSE
TDSC,NASDAQ
CWST,NASDAQ
BDC,NYSE
EDN,NYSE

create a following perl script and save it as fetch-stock-price.pl:

#!/usr/bin/perl

use Finance::Quote;

my $q = Finance::Quote->new();
open (FIN, $ARGV[0]) || die ("Could not open $ARGV[0]");

while ($line = <FIN>) { ($field1,$field2) = split ',', $line;

my %hash = ( $field1 => $field2 ); chomp(%hash); foreach my $i (keys %hash) { my %data = $q->fetch( $hash{$i}, $i); print $hash{$i} . " " . $i . ": " . $data{$i, 'price'} . "\n"; } }
close (FIN);

At this point we need this script executable and execute it with an argument stocks.csv:

$ chmod +x fetch-stock-price.pl
$ ./fetch-stock-price.pl stocks.csv

OUTPUT:

NASDAQ AAON: 25.10
NYSE CMS: 17.95
NASDAQ TDSC: 27.10
NASDAQ CWST: 4.54
NYSE BDC: 33.06
NYSE EDN: 10.21


Free Linux eBooks

Go to top