Perl 处理键盘中断

Perl handling Keyboard Interrupt

我正在尝试编写一种方法,当用户键入 CTRL+C 时,脚本将在关闭前继续执行几个步骤。我知道如何在 Python 中执行此操作。 python 代码将是这样的:

try:
    workbook = xlsxwriter.Workbook('Desktop\ODFU '+str(System)+'.xlsx')
    worksheet = workbook.add_worksheet()
    excelcreate(System)
    login(USER, PASS)
    test()
    time.sleep(2)
    print"Test is complete..."
    logout()
except KeyboardInterrupt: # CTRL+C Terminates script and saves everything and logs out
    print""
    print "Interruption occurred!! Script terminating..."
    workbook.close()
    logout()    

如何使用 Perl 获得与此完全相同的东西?

只需定义一个 SIGINT 处理程序:

local $SIG{INT} = sub {
    $workbook->close;
    logout();
    exit 130
};