Hello my esteemed code fellows.

I have this script that parses uploaded excel files into tab delimited .txt versions. The problem is that from time to time some user shows up with a corrupted xls file, and the Spreadsheet::ParseExcel gets stuck at the conversion. I'd like to set up a timeout, but the code below isn't working. Can any of you guys give me a hand here?

#die 1; ###################### # File conversion: xls->txt ####################### local $SIG{ALRM} = sub { die("TIMED OUT") }; eval { alarm(10); #set our alarm # sleep 5; # with this sleep, the alarm works. But with the code bel +ow it doesn't... if ( $extension eq "xls") { my $infile = "../thissite/myuploads/$user/$systemname"; my $outfile = "../thissite/myuploads/$user/inventory.txt"; # Requesting extra modules: use Spreadsheet::ParseExcel; use Data::Dumper; # Openning the file dump open (DUMP, ">$outfile") || warn "Error opening file: $!\n"; select DUMP; # Excel parsing my $prev_index = -1; my $prev_row = 0; my $prev_col = 0; my $cell_handler = sub { my $workbook = $_[0]; my $sheet_index = $_[1]; my $row = $_[2]; my $col = $_[3]; my $cell = $_[4]; # Only process the first worksheet if ($sheet_index > 0) { $workbook->ParseAbort(1); return; } # Reset the col counter between rows $prev_col = 0 if $row != $prev_row; # Add tabs between fields and newlines between rows. Also pa +d # any missing rows or columns. # print "\n" for $prev_row +1 .. $row; print "\t" for $prev_col +1 .. $col; # Print the UNformatted value of the cell print $cell->{Val}; # Keep track of where we are $prev_row = $row; $prev_col = $col; }; my $parse_excel = new Spreadsheet::ParseExcel(CellHandler => $ce +ll_handler, NotSetCell => 1); my $workbook = $parse_excel->Parse("$infile"); ############ ############ alarm(0); close DUMP; select STDOUT; } }; if ( $@ =~ /TIMED OUT/ ) { die "CONVERSION TIME EXCEEDED. FILE MAY BE CORRUPT!"; } #die 2;

The 'die 1' indicates me that the code runs just fine exactly untill before this conversion. And the 'die 2' indicates me that it won't pass through it.

I tested the alarm with the 'sleep 5', and it works just fine. But with the excel-parsing code it doesn't.

Any ideas?

I even tried setting the alarm around the line that activates the parsing, but it didn't work either.

local $SIG{ALRM} = sub { die("TIMED OUT") }; eval { alarm(10); #set our alarm + my $workbook = $parse_excel->Parse("$infile"); alarm(0); };

Thanks a lot, my friends

André


In reply to Timeout for parsing corrupted excel files by Andre_br

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.