isakbob
isakbob

Reputation: 1549

Is there an option in the perl command to check for undefined functions?

Background

The perl command has several idiot-proofing command line options described in perldoc perlrun:

-c causes Perl to check the syntax of the program and then exit without executing it.
-w prints warnings about dubious constructs, such as variable names that are mentioned only once and scalar variables that are used before being set, etc.
-T forces "taint" checks to be turned on so you can test them.

After reading through these options, I could not find one that detects undefined functions. For example, I had a function I used called NFD() that imports the Unicode::Normalize package. However, being a perl novice, I did not know if this was already under the fold of the standard perl library or not. And perl -c nor any of the other options uncovered this error for me, rather a coworker noticed that it was somehow undefined (and not inside the standard libraries). Therefore, I was curious about the following:

Question

Is there an option in the perl command to automatically detect if there is an undefined function not already inside an imported package?

Upvotes: 2

Views: 341

Answers (2)

brian d foy
brian d foy

Reputation: 132896

Aside from the particular technical details, you can't know if a function will be defined at some time in the future when you plan to use it. As a dynamic language, thinks come into and go out of existence, and even change their definitions, while the programming is running.

Jeffrey Kegler wrote Perl Cannot Be Parsed: A Formal Proof that relied on this idea. The details of the halting problem aren't as interesting as workings of a dynamic language.

And, for what it's worth, those command-line options don't make programs idiot-proof. For example, in Mastering Perl I show that merely adding -T to a program doesn't magically make it secure, as many would have you believe.

What were you doing with Unicode::Normalize? It has an NFD already but your question makes it sound like you were wrapping it somehow:

use Unicode::Normalize qw(NFD);

Upvotes: 1

ikegami
ikegami

Reputation: 386541

I did not know if this was already under the fold of the standard perl library or not.

It sounds like you want to distinguish imported subs from other subs and builtin functions.

If you always list your imports explicitly instead of accepting the defaults like I do, then you'll not only know which subs are imported, you'll know from which module they were imported.

use Foo::Bar;                 # Default imports
use Foo::Bar qw( );           # Import nothing  ("()" also works)
use Foo::Bar qw( foo bar );   # Import subs foo and bar.

Is there an option in the perl command to check for undefined functions?

On the other hand, if you are trying to identify the subs that you call that don't exist or that aren't defined at compile time, then this question is a duplicate of How can I smoke out undefined subroutines?.

Upvotes: 5

Related Questions