Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Stock Quotes

by scribe (Initiate)
on May 24, 2000 at 20:39 UTC ( #14594=sourcecode: print w/replies, xml ) Need Help??
Category: Utility / Web
Author/Contact Info
Description: Retrieves basic stock quotes from yahoo.
Uses IO::Socket to connect to webserver.
Can only retrieve one quote at a time now.

Sample output:
(( ELON 51.625 -3.5625 ) ( 49 -- 56.5 ) ( Vol: 1646200 ))
#!/usr/bin/perl -w
# Steven Rutter <>

use IO::Socket;
use strict;

if ($#ARGV < 0) { die "usage: $0 <symbol>\n" }

my $symbol = $ARGV[0];
my $server = '';
my $serverPort = '80';
my $get = "/d/quotes.cgi?s=$symbol&f=sl1d1t1c1ohgv&e=.csv";

my $remote = new IO::Socket::INET (
                  Reuse=>1 ) or die $!;

$remote -> autoflush(1);

print $remote "GET $get HTTP/1.0\n\n";

my $quote;

while ( my $raw = <$remote>) {

  # "USVOE.OB",2.75,"4/12/2000","4:00PM",-0.3125,3.0625,3.160000,2.656
  # symb, price, lastTrade, lastTradeTime, change, high, low, volume

  if ( $raw =~ /\"/ ) {
    $quote = $raw;

close $remote;

$quote =~ s/\"//g;
$quote =~ s/^M//g;
chomp $quote;
my @qarray = split(/,/, $quote);

print "(( $qarray[0] $qarray[1]  $qarray[4] ) ( $qarray[7] -- $qarray[
+6] ) ( Vol: $qarray[8] ))\n";
Replies are listed 'Best First'.
RE: Stock Quotes
by KM (Priest) on May 24, 2000 at 20:57 UTC
    This wheel has already been invented, very well in fact. Look at Finance::YahooQuote and Finance::QuoteHist and Finance::Quote.


      I'm hesitant to use too many non-standard modules.
      This may be because I am new to this.

      Is it common practice to use all the modules you
      can? I worry about it's portability then - what if the
      system doesn't have the module installed and the
      admins are slow/reluctant to install it?


      plus I had fun ;)
        It is good to have fun and learn. And, have fun learning. One of the reasons I have done Perl all these years is that I always have fun with it, unlike other languages where I didn't find it fun to be using and writing.

        Anyways, don't be hesitant to use modules that aren't part of the distrobution. There is a lot of useful modules out there that will save you time and headaches. To learn, it is always good to read the modules POD and source to see what makes it tick.

        Unless you are using a module which is specific to Win32, or *nix specific, then don't worry about portability. Many modules work fine on many platforms. If you have a reluctant sysadmin, you can still install modules under your home directory. Then you can change @INC to look there, or PERL5LIB.

        In order to allow users to install their own modules I have sometimes aliased 'perl' with 'perl -I/path/to/their/modules' which has worked nicely.

        Hope this helps.


        I wholeheartedly agree with KM. Just for the sake of completeness, I will mention some arguments against using non-standard modules:
        • Learning how to do something yourself may be a worthwhile learning experience.
        • The module that does what you need may include a lot of extra functionality that you are not going to use. In those cases, sometimes it's good to peek under the hood and only extract the functionality you need (giving proper credit, and ideally asking for permission from the author) and putting it directly in your program
        Of course, this only applies to modules that do relatively simple things. If you need to do database access or write a CGI script. By all means, please, use the modules. There's no need to reinvent the wheel, particularly when it's a complex wheel.


Re: Stock Quotes
by mga (Initiate) on Mar 07, 2001 at 20:11 UTC
    hello i downloaded the code and uploaded to my server (haven't modified yet... just testing) and i get the following error: apache: [Wed Mar  7 10:03:33 2001] [error] [client] Premature end of script headers: /u/web/alleat/cgi-local/ can't make the darn thing work... any help will be appreciated

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://14594]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-09-28 09:33 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (143 votes). Check out past polls.