Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

trap error from parsexlsx

by gauss76 (Scribe)
on Oct 02, 2017 at 08:59 UTC ( [id://1200508]=perlquestion: print w/replies, xml ) Need Help??

gauss76 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All

I have the following problem. When I try to open an Excel xlsx file that is actually an old .xls type file with the perl module Spreadsheet::ParseXLSX my program crashes with an error. I have tried putting the command in an eval statement as below but this does not work. Is there any way to trap the error?

$FileToOpen=myfile.xlsx;

my $parser=Spreadsheet::ParseXLSX->new();

my $xxx=eval{$parser->parse($FileToOpen)};

When I run the code I get a load of errors from the $parse statement followed by the result of $xxx is:Spreadsheet::ParseExcel::Workbook=HASH(0x5c05968)

Am I using eval incorrectly or is there something else I am missing?

Many thanks in advance for any help with this.

Replies are listed 'Best First'.
Re: trap error from parsexlsx
by poj (Abbot) on Oct 02, 2017 at 12:38 UTC

    An alternative would be to test the file type first and then choose the correct parser. For example

    #!/usr/bin/perl use strict; use File::MMagic; use Spreadsheet::ParseXLSX; use Spreadsheet::ParseExcel; my $FileToOpen = 'c:/temp/myfile.xlsx'; my $ft = File::MMagic->new(); my $type = $ft->checktype_filename($FileToOpen); my $parser; if ($type =~ /zip/){ $parser = Spreadsheet::ParseXLSX->new(); } else { $parser = Spreadsheet::ParseExcel->new(); } my $workbook = $parser->parse($FileToOpen); if ( !defined $workbook ) { die $parser->error(), ".\n"; } printf "%s has %d sheets\n",$FileToOpen,$workbook->worksheet_count();
    poj
Re: trap error from parsexlsx
by Corion (Patriarch) on Oct 02, 2017 at 09:00 UTC

    What errors do you get? Maybe they are trying to tell you something?

Re: trap error from parsexlsx
by thanos1983 (Parson) on Oct 02, 2017 at 09:33 UTC

    Hello gauss76,

    You can also try Spreadsheet::Read, there is also a nice short tutorial about it How to read an Excel file in Perl.

    But to be honest most likely the error that you are getting will tell you what is wrong with your file. Try to create a new xlsx file e.g. test.xlsx insert one or two lines of data and try to open it with your script. If this works then your other file is corrupted and it can not be processed (most likely).

    Hope this helps, let us know if your problem get's resolved. BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: trap error from parsexlsx
by kcott (Archbishop) on Oct 03, 2017 at 06:18 UTC

    G'day gauss76,

    Your problem starts with the first line of code you posted:

    $FileToOpen=myfile.xlsx;

    It seems to work as is:

    $ perl -e '$FileToOpen=myfile.xlsx;' $

    But when I ask Perl to warn about problems:

    $ perl -we '$FileToOpen=myfile.xlsx;' Unquoted string "myfile" may clash with future reserved word at -e lin +e 1. Unquoted string "xlsx" may clash with future reserved word at -e line +1. Name "main::FileToOpen" used only once: possible typo at -e line 1.

    If I use strict:

    $ perl -Mstrict -e '$FileToOpen=myfile.xlsx;' Global symbol "$FileToOpen" requires explicit package name (did you fo +rget to declare "my $FileToOpen"?) at -e line 1. Bareword "myfile" not allowed while "strict subs" in use at -e line 1. Bareword "xlsx" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.

    If I ask Perl how it sees that code:

    $ perl -MO=Deparse -e '$FileToOpen=myfile.xlsx;' $FileToOpen = 'myfilexlsx'; -e syntax OK

    [Hint: See the concatenation operator in "perlop: Additive Operators".]

    Add these lines to the start of your code:

    use strict; use warnings;

    Then fix all the code such that you no longer get any error or warning messages.

    Use the strict and warnings pragmata in all of your code!

    — Ken

Re: trap error from parsexlsx
by Anonymous Monk on Oct 02, 2017 at 13:56 UTC
    If $xxx is getting set, then the parse is not dying. Those must be warnings that you're seeing, not errors. There may or may not be an easy way to suppress warnings in this case, but I don't feel like hunting it up. Maybe someone else knows the answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1200508]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-25 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found