Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I accidentally tried to assign a non-existing function to $SIG{__DIE__}. It took some time to find that bug, because perl did not emit any warnings or errors.

My code, stripped down to show the problem:

#!/usr/bin/perl -T -w use strict; use warnings; $SIG{'__DIE__'}=\&noSuchFunction; print "You should not see this text\n"; # but you will!

Is it a bug in perl (v5.8.8)? I think I should not be able to create and assign a reference to a non-existing function, at least not with strict and warnings enabled.

Using 'noSuchFunction' instead of \&noSuchFunction did not help (and is not recommended in perlvar.pod), only *noSuchFunction generated a warning.

Thanks!

Replies are listed 'Best First'.
Re: Reference to non-existing function without warning or error
by ikegami (Patriarch) on Aug 02, 2007 at 15:09 UTC

    Nothing prevents you from taking the reference of a scalar, array, hash, glob, function or whatever that hasn't been declared.

    use strict 'vars'; will prevent you from mentiong variables that haven't been declared, but it will neither prevent you from mentioning a sub that hasn't been declared, nor prevent you from making a reference to something that doesn't exist.

    I don't see any reason to allow making a reference to a function that don't exist. It could simply be a case that noone thought to make special. Or maybe they didn't see the need to make it special since Perl usually catches the problem when the code attempts to execute the referenced non-existing function. If that's the case, one could argue the bug is in die.

    Using 'noSuchFunction' instead of \&noSuchFunction did not help (and is not recommended in perlvar.pod), only *noSuchFunction generated a warning.

    Odd. I would expect it to give no warning. In fact, I can't replicate your result. I can't get *noSuchFunction to give a warning.

    use strict; use warnings; $_ = \&noSuchFunction; print("$_\n"); $_ = *noSuchFunction; print("$_\n"); $_ = *noSuchFunction{CODE}; print("$_\n"); $_ = \*noSuchFunction; print("$_\n");

    outputs

    CODE(0x225e58) *main::noSuchFunction CODE(0x225e58) GLOB(0x226b9c)
      The warning was 'Name "main::noSuchFunction" used only once: possible typo', because I used that name only once (typically for a typo). It was not a warning about referencing an undefined function.