编译以下代码时出现2个错误:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::ParseExcel;
my $xlsparser = Spreadsheet::ParseExcel->new();
my $xlsbook = $parser->parse('xsl_test.xls');
my $xls = $xls->worksheet(0);
my ( $row_first, $row_last ) = $xls->row_range();
my ( $col_first, $col_last ) = $xls->col_range();
my $csv = '';
for my $row ( $row_first .. $row_last ) { #Step through each row
for my $col ( $col_first .. $col_last ) { #Step through each column
my $cell = $xls->get_cell( $row, $col ); #Get the current cell
next unless $cell;
$csv .= $cell->unformatted(); #Get the cell's raw data -- no border colors or anything like that
if ( $col == $col_last ) {
$csv .= "\n"; #Make a new line at the end of the row
} else {
$csv .= ",";
}
}
}
错误:
global symbol "$parser" requires explicit package name at line 8
global symbol "$xls" requires explicit package name at line 9
我从http://www.ehow.com/how_7352636_convert-xls-csv-perl.html获取上述代码,并使用以下代码安装了excel模块:cpan Spreadsheet::ParseExcel Spreadsheet::XLSX Spreadsheet::Read
导致错误的原因是什么?
答案 0 :(得分:3)
这些错误意味着您使用的是strict
,但您没有使用my
声明某些变量。例如,您声明了$xmlprser
,但之后您尝试使用未声明的$parser
。您复制的代码有错误。
获取代码的更好的地方是源本身:Spreadsheet::ParseExcel
尝试:
my $parser = Spreadsheet::ParseExcel->new();
my $xlsbook = $parser->parse('xsl_test.xls');
my $xls = $xlsbook->worksheet(0);