This a stock quote cgi application in Perl. This script was developed for ACT Manufacturing

and delivers 15 minute delayed stock information (since they declared bankruptcy, this script was taken down). The data source is a public site: XML Today

. The script will parse out a template page written in standard html. This separation of the script from the template allows designers to make changes without affecting the functionality of the application.

download taking 5 dvdrip

#!/usr/local/bin/perl
# quote.cgi: Perl script to retrieve stock quote data.
# Greg Rushton
# 3/30/2000

use LWP::Simple;

$symbol = "ACTM";
$url = "http://www.xmltoday.com/examples
		/stockquote/getxmlquote.vep?s=$symbol";
$xml = get($url);
$xml =~ s/012015?|015/n/g;
$templ_loc = "/path/to/cgi-bin/nasdaq.htm";
$template = "";

&parse_xml;
&get_template;
&return_page;
#&test;

sub return_page {
	$template =~ s/<<ask>>/$ask/m;
	$template =~ s/<<open>>/$open/m;
	$template =~ s/<<dayhigh>>/$dayhigh/m;
	$template =~ s/<<daylow>>/$daylow/m;
	$template =~ s/<<change>>/$change/m;
	$template =~ s/<<volume>>/$volume/m;
	$template =~ s/<<date>>/$date/m;
	$template =~ s/<<time>>/$time/m;
	print "Content-type: text/html", "\n\n";
	print $template;
}

sub get_template {
	open (TEMPLATE, "$templ_loc") ||
		die "Cant open $template: $!";
	flock (TEMPLATE, 1) || die "Cant flock $template: $!";
		while (<TEMPLATE>) {
			$template .= $_;
		}
	close (TEMPLATE) or die "cant close $template: $!";
}

sub parse_xml {
	@lines = split (/n/, $xml);
	foreach (@lines) {
		if (m/<date>/) {
			($date) = />(.*?)</;
		}
		if (m/time/) {
			($time) = />(.*?)</;
		}
		if (m/ask/) {
			($type, $value) = /"(.*?)"+/g;
			$ask = $value;
		}
		if (m/open/) {
			($type, $open) = /"(.*?)"+/g;
		}
		if (m/dayhigh/) {
			($type, $dayhigh) = /"(.*?)"+/g;
		}
		if (m/daylow/) {
			($type, $daylow) = /"(.*?)"+/g;
		}
		if (m/change/) {
			($change) = />(.*?)</;
		}
		if (m/volume/) {
			($volume) = />(.*?)</;
		}
	}
}

sub test {
	#print $xml, "\n";
	print "Content-type: text/html", "\n\n";
	print $symbol, "\n";
	print $date, "\n";
	print $time, "\n";
	print $ask, "\n";
	print $open, "\n";
	print $dayhigh, "\n";
	print $daylow, "\n";
	print $change, "\n";
	print $volume, "\n";
}

exit(0);

: Perl

Leave a Reply

Previous Post
«
Next Post
»