in reply to determine file type from data read from filehandle

You should never trust the file extension even if it's there. You can try File::Type or File::MMagic. I would probably just do something like (first-draft logic, untested)–

if ( "ok" eq eval { process_upload_as_excel($upload); "ok" } ) { # Do stuff, report success. } elsif ( $@ ) { my $first_err = $@; if ( "ok" eq eval { process_upload_as_cvs($upload); "ok" } ) { # Do stuff, report success. } else { die "Couldn't process as Excel or CVS..."; # Include $first_err and (new one) $@. Or don't. # I'm a code comment, not a cop. } } else { die "Unknown failure!"; }

Replies are listed 'Best First'.
Re^2: determine file type from data read from filehandle
by Eily (Monsignor) on Aug 07, 2018 at 16:42 UTC

    While I agree that CSV files do come with all kinds of extensions, including none or even .xls, Excel comes from a world where file extension matters. So I wouldn't process an Excel file with a .txt extension, even if it worked, because if I received such a file (or an .xlsx file that does not contain Excel data), I'd think there's something wrong happening with the input data.

      I definitely see the point but for it to matter in fact you have to come up with a .txt or any other file type that will pass Excel parsing without error and return a workbook with content. That seems, to me, like a concern that can be safely punted to: "Failed to import foo.bar: because reasons..."