Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circumstances under which die() does not exit a Perl script?

Tags:

perl

I'm debugging a really weird problem with a long-running Perl script.

The problem is that the script does not exit on die() as expected. Instead the script just hangs without returning.

I've not defined any error handlers myself so I would assume that die() would lead to an immediate termination of the script.

This is the basic structure of the script and the modules used:

#!/usr/bin/perl

use strict;
use utf8;
use warnings;

use DBI; # with MySQL driver ("dbi:mysql:database=...")
use Geo::IP;
use POSIX;
use URI::Escape;

open(COMMAND, 'command_line |');
while (<COMMAND>) {
    #
    # .. stuff that can go wrong ..
    #
    die("I'm expecting the script to terminate here. It doesn't.") if ($gone_wrong);
}
close(COMMAND);

What could be the explanation to this behaviour? Is any of the modules used known to set up error handlers that could explain the script hanging on die()?

like image 672
knorv Avatar asked Jul 13 '11 22:07

knorv


People also ask

What does die function do in perl?

The die() function can be used to stop the script and can be used to display a message to the end user. It can be used at the right side of the OR ( || ) operator and at left side can be any expression like the eval() function.


1 Answers

Well, END blocks and object destructors are still called after a die. If one of those hangs (or does something that takes a long time), the script won't exit immediately. But that should happen after printing the message from die (unless STDERR is buffered so you don't see the message immediately).

You mention DBI, so you probably have a database handle whose destructor is being called. (I'm not sure that's the problem, though.)

like image 136
cjm Avatar answered Oct 29 '22 01:10

cjm