CGI and Perl

Listing 9.2. Automatic stock quote retriever (

 require LWP::UserAgent;
 require HTTP::Request;
 $symbols=join(`+', @ARGV);
 $ua = new LWP::UserAgent;
 $request = new HTTP::Request `GET', $url;
 while (1) {
    $response = $ua->request($request);
    if ($response->is_success) {
    } else {
    # We want to receive quotes every hour.
    sleep $anHour;

As you can see, the symbols are passed in as arguments to this Perl script. They are then joined into a single string with each symbol separated by a plus sign. This string is appended to the URL creating the full URL request for the specified stock symbols. This leaves only the handleResponse() and handleError() subroutines left to implement. handleError() is rather easy. HTTP::Response provides a method called error_as_HTML(), which returns a string containing the error nicely packaged as an HTML document. You either can print the HTML as it is or ignore the error and continue. In this example, we will just fail silently and continue.

Handling the response should be fairly straightforward given the sample HTML you've already seen in Listing 9.1. You simply need to write a loop that looks for the quote indicator strings, which are </center><pre> and </pre><center>. These strings indicate where you are in the document. You can then use regular expressions to parse out the symbol, current trading price, and other important values. The handleResponse() is implemented in Listing 9.3. The output appears in Figure 9.2.

Listing 9.3. Subroutine to extract the quote information.

sub handleResponse {
    foreach (@lines) {
       if ($insideQuote) {
          if (/<\/pre><center>/) {
             print "$symbol on $exchange is trading at $value on $dateTime\n";
          } elsif (/^Symbol\s*:\s*(\S*)\s*Exchange\s*:\s*(.*)\s*$/) {
          } elsif (/^Last Traded at\s*:\s*(\S*)\s*Date\/Time\s*:\s*(.*)$/) {
       if (/<\/center><pre>/) {

Of course, you can add more code to parse out other returned values, such as the 52-week low and high values. This would involve just adding another elsif block and a regular expression to match the particular pattern.

by BrainBellupdated