Having puttered, I share:
#!/usr/bin/perl -w
use strict;
use English;
use vars qw($boo);
$a = foo() if 0;
sub bar {}
END {
no strict 'refs';
my $table = '::';
SYMBOL:
for my $sym (sort {lc($a) cmp lc($b)} keys %{$table}) {
next if $sym =~ /::$/ or $sym =~ /^[A-Z0-9]/;
my $name = $table . $sym;
next if $name =~ /^::\W/;
foreach my $thing qw(ARRAY HASH IO) {
if (defined *{$name}{$thing}) {
next SYMBOL;
}
}
print "Look at this!\n";
unless (defined *{$name}{CODE}) {
warn "Possible undefined routine '$sym'";
}
}
}
The problem is that *{$name}{CODE} is as expected, undef for symbol foo, and not undef for symbol bar, but it is also undef for symbol boo.
That is, there's no way to distinguish at the symbol table level between an undefined subprogram name, and a scalar, because both will have
defined(*foo{SCALAR}) && !defined(*foo{CODE})
Hmph.
| -- |
| Jeff Boes |
| Database Engineer |
| Nexcerpt, Inc. |
|
|
|
...Nexcerpt...Connecting People With Expertise
|
| [reply] [d/l] [select] |
I think this is a fantastic idea! I suspect that there is a way to distinguish these cases if someone dives a little deaper into the internals.
I'd never thought of this idea, but upon hearing it, it is such a good idea that I'd be a bit surprised if someone hasn't already tried this and perhaps succeeded (or figured out exactly why you can't do it).
Anyway, I'm very interested in seeing this tool become available. Please consider posting this as a new root node, especially if you don't get any useful responses in the next while.
Thanks much. I'm looking forward to being able to do:
perl -MDevel::ShowUndefSubs -c myScript
- tye | [reply] [d/l] |
When I test it, it does not seem that END blocks run under -c. However a CHECK block will - and the line, return unless $^C; will properly make it only run if you are running under -c.
Also it is a horrible hack, but you can test for scalars that have been declared with vars by evaling a piece of code which switches to that package and tries to access that variable. You can also test if the variable itself has a value. This doesn't, unfortunately, catch variables initialized with our. (Well I don't like our so I don't consider it that unfortunate...) However it catches vars, and it catches the common case of modules that initialize @ISA, etc while declaring them with our. (You also clear out a couple of global variables in main:: which are used by Perl internally.)
If you turn this into a module then I would think carefully about how you want to control what will be checked for errors, allowing someone to readily decide that certain packages and functions are known to be OK, or only certain ones should be checked. After all the maximum utility will happen when you can check your code, see some errors, and then OK them as you figure out that they really are OK...
| [reply] [d/l] |
| [reply] |
I came across B::Lint, but it won't catch
things like --
use strict;
my $a = foo() if 0;
in that it still *runs* the code in question, and only flags undefined routines if they are actually *called*. Oh, well.
| -- |
| Jeff Boes |
| Database Engineer |
| Nexcerpt, Inc. |
|
|
|
...Nexcerpt...Connecting People With Expertise
|
| [reply] [d/l] [select] |