0

In my perl script (which actually runs as daemon), I accidentally tried to access an array element(which is also an array) which was not defined before.My script is exiting at this point but is not dumping any core.Is it possible to dump core in this case? I tried killing my perl process by kill -6 command,the core gets generated in this case But I want to generate core in every instance when we exit unexpecteldy from script.

my @array = ();
my @a;
@a = @{$array[1]};
  • 1
    Accesing undefined array element doesn't exit in Perl: `perl -e 'print $arr[20]; print "still here"'` – choroba Mar 02 '17 at 09:47
  • 1
    Still no error for me. – choroba Mar 02 '17 at 10:01
  • 2
    "dump core" is not usually one of the steps in debugging a perl script. – mob Mar 02 '17 at 14:46
  • One way to always get something is to put it in the `END` block. It runs at every `die` or `exit`. But it may not be specific enough. Another way is to "protect" against errors as you code along -- use careful error checking. Then, you can get very specific information. Or, override the `die` but providing a hook (subroutine) for `$SIG{__DIE__}` "signal". See for example [this answer](http://stackoverflow.com/a/42183690/4653379) for a debugger that fires off on any `die`, in a dozen lines of code. – zdim Mar 03 '17 at 04:48

1 Answers1

0

Instead generate a core dump or something similar, I would recommended you log messages in your script and do some checks specially when you are trying to access something that does not exists.

Using a source level debugger as suggested in perldoc are for some rare cases:

If the debugging output of -D doesn't help you, it's time to step through perl's execution with a source-level debugger.

We'll use gdb for our examples here; the principles will apply to any debugger (many vendors call their debugger dbx ), but check the manual of the one you're using.

Trying to access a position of an array that does not exists can be avoided by using if and else for example:

my $wanted_position = 2;

if ( $wanted_position >= @array )
{

    #log a possible error
    #Do something else
}

@a = @{$array[$wanted_position]};

You can handle signals inside your script also.

Perl uses a simple signal handling model: the %SIG hash contains names or references of user-installed signal handlers. These handlers will be called with an argument which is the name of the signal that triggered it. A signal may be generated intentionally from a particular keyboard sequence like control-C or control-Z, sent to you from another process, or triggered automatically by the kernel when special events transpire, like a child process exiting, your own process running out of stack space, or hitting a process file-size limit.

Example:

 $SIG{DIE} = sub { #dump code here };
carlosn
  • 423
  • 3
  • 9