In addition to
perrin's advice, I'd be specific about what you're looking for in $@. There are many reasons why an
INSERT could fail. Some you might be able to proceed past (e.g., duplicate key errors), and some are show stoppers (e.g., database or transaction log full).
Change
if ( $@ ) { ...
to
if ( $@ =~ /specific error text/ ) { ...
You might need several tests to handle different error conditions.