Saturday, 14 September 2013

PERL Mechanize cannot print results when reading input data from a data file

PERL Mechanize cannot print results when reading input data from a data file

I created a PERL program to retrieve the stock exchanges from Yahoo
Finance, given a list of stock symbols.
The following code writes to a file
#!/usr/bin/perl
# program name: FindStockExchange.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange.pl A AA AA.V AAA.TO -f ~/symbol_out.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -s Symbol
# -f Output filename
# Initialize variables:
my $urlBase='http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix='&ql=0'; # After symbol
my $url='';
my $oFile='';
my $symbol='';
my $c='';
# Read command line options.
GetOptions(
'f=s' => \$oFile #Output filename
) or die "Incorrect usage!\n";
# Ouptput file(s)
open(OUTSYM,">$oFile") || die "Couldn't open file $oFile, $!";
my $m = WWW::Mechanize->new( autocheck=>0 );
foreach $symbol ( @ARGV ){
$url=$urlBase . $symbol .$urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span
class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets
</span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
} # End foreach
close OUTFIL;
The following code reads from an input file and creates an empty data
file. The input file contained the following stock symbols:
A AA AA.V AAA.TO
#!/usr/bin/perl
# program name: FindStockExchange2.pl
use strict;
use warnings;
use WWW::Mechanize;
use Storable;
use Getopt::Long;
#cmd: clear; ./FindStockExchange2.pl -i ~/symbol_in.txt -o ~/symbol_out2.txt
# Find Stock Exchange for a given Stock Symbole
# Command line options:
# -i Input filename
# -o Output filename
# Initialize variables:
my $urlBase='http://finance.yahoo.com/q?s='; # Before symbol
my $urlSuffix='&ql=0'; # After symbol
my $url='';
my $oFile='';
my $iFile='';
my $symbol='';
my $c='';
# Read command line options.
GetOptions(
'o=s' => \$oFile, #Output filename
'i=s' => \$iFile #Input filename
) or die "Incorrect usage!\n";
# File(s)
open(OUTSYM,">$oFile") || die "Couldn't open file $oFile, $!";
open(INSYM,"<$iFile") || die "Couldn't open file $iFile, $!";
my $m = WWW::Mechanize->new( autocheck=>0 );
while ( <INSYM> ){
$symbol=chomp($_);
$url=$urlBase . $symbol .$urlSuffix;
$m->get($url);
$c = $m->content; # Places html page source text into variable
# Text pattern: <div class="title"><h2>Electrolux AB (ELUXY)</h2> <span
class="rtq_exch"><span class="rtq_dash">-</span>OTC Markets
</span></div>
$c =~ m{rtq_dash\">-</span>(.*?)</span>}s or next;
print OUTSYM "$symbol\t$1\n"; # Write output file
print "$symbol\t$1\t" . "\n"; # Write to STDOUT
} # End while
close INSYM;
close OUTSYM;
Why would changing from a foreach loop to reading an input file using a
while loop produce different results?
Foreeah code creates a file containing the following: A NYSE
AA NYSE
AA.V TSXV
AAA.TO Toronto
To-Air-Is:~ vlis
While loop creates an empty file.

No comments:

Post a Comment