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

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top