All pastes #188531 Raw Edit

Untitled

public text v1 · immutable
·published 2006-10-02 06:59 UTC
rendered paste body
#!/usr/bin/perl

use warnings;
use strict;

use LWP::Simple;
use Text::xSV;
use File::Temp qw/ tempfile /;


# Retrieves a CSV file and saves it to a temporary file (RAM location?)
sub _get_csv {
    my $ticker = shift @_;
    my ($fh, $filename) = tempfile();
    open($fh, "+>", $filename);

    # Change to sprintf
    my $data = get(
	"http://finance.yahoo.com/d/quotes.csv?s="
	. $ticker
	. "&f=sl1d1t1c1ohgv&e=.csv");

    die "Couldn't get CVS file for stock $ticker!" unless defined $data;
    
    print $fh $data;
    return $filename;

}

my $csv = new Text::xSV;

# Display data for each stock ticker specified
foreach my $ticker (@ARGV) {
    my $filename = _get_csv($ticker);
    $csv->open_file($filename);
    my @fields = $csv->get_row;
    foreach my $field_in (@fields) {
	print $field_in . "\n";
    }
}