Perl - 股票代码错误:未定义的提取方法

时间:2015-09-19 14:46:20

标签: perl

我正在为我班级的股票行情计划工作。该程序应从文件中读取自动收报机列表,使用Finance::Quote模块获取信息并将结果显示在屏幕上。当我运行程序时,收到的错误如下: Undefined fetch-method (ticker) passed to Finance::Quote::fetch at filename.pl line 56.

我整天都在搜索这个错误原因,但我无法找到任何帮助我理解问题的答案。任何人都可以帮忙吗?

我的代码:

#!/usr/bin/perl -w
#
use strict;
use Finance::Quote;

# Set preferred currency
my $CURRENCY = "CNY";   

# Assign file name to scalar variable, read file, and then assign 
# to an array
my $techticker = "techticker.txt";
open (FH, "< $techticker") or die $!;
my @STOCKS = <FH>;
print @STOCKS, "\n";
close FH or die "Cannot close $techticker: $!"; 

# Define the format : label, style formatting, width of the field
my @labels = (["name",  "%24s",  15],
           ["date",  "%11s",  17], 
           ["time",  "%8s",  10],
           ["last",  "%8.2f",  8],
           ["high",  "%8.2f",  8], 
           ["low",   "%8.2f",  8],
           ["close", "%8.2f",  8], 
           ["volume","%10d",  10]);

# Seconds between refresh
my $REFRESH = 120;  

# --- END CONFIG SECTION ---

my $quoter = Finance::Quote->new();
my $clear  = `clear`;           # So we can clear the screen.

# Build our header.

my $header = "\t\t\tMEGA CAPITAL TECHNOLOGY COMPANY STOCK REPORT" .    ($CURRENCY ? " ($CURRENCY)" : "") ."\n\n";

foreach my $tuple (@labels) {
    my ($name, undef, $width) = @$tuple;
    $header .= sprintf("%".$width."s",uc($name));
 }

$header .= "\n".("-"x85)."\n";

# --- END HEADER SECTION ---

# Set default currency.
$quoter->set_currency($CURRENCY) if $CURRENCY;  


for (;;) {  # For ever.
    print $clear,$header;

    foreach my $stockset (@STOCKS) {
        my ($exchange, @symbols) = $stockset;           # I think the problem is here
        my %info = $quoter->fetch($exchange,@symbols);

        foreach my $symbol (@symbols) {
            next unless $info{$symbol,"success"}; # Skip failures.
            foreach my $tuple (@labels) {
                my ($label,$format) = @$tuple;
                printf $format,$info{$symbol,$label};
            }
            print "\n";
        }
    }

    sleep($REFRESH);
}

更新: 只是修复了我的代码,我应该提取我的数据并将其分配给$ exchange;&#34; foreach&#34;之外的$ exchange和@symbols。环。以下是固定代码:

my $techticker = "techticker.txt";
open (FH, "< $techticker") or die $!;
my @symbols = <FH>;
chomp @symbols;
my $exchange = shift @symbols;

close FH or die "Cannot close $techticker: $!"; 

...

my %info = $quoter->fetch($exchange,@symbols);

for (;;) {  # For ever.
    print $clear,$header;

    foreach my $symbol (@symbols) {
        next unless $info{$symbol,"success"}; # Skip failures.
        foreach my $tuple (@labels) {
            my ($label,$format) = @$tuple;
            printf $format,$info{$symbol,$label};
        }
        print "\n";
    }

    sleep($REFRESH);
}

0 个答案:

没有答案