find_undefined_methods.pl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/perl
  2. # Find undefined methods/symbols in a lib tree.
  3. use utf8;
  4. use 5.010;
  5. use strict;
  6. use autodie;
  7. use warnings;
  8. use lib qw(.);
  9. use open IO => ':encoding(UTF-8)';
  10. use File::Find qw(find);
  11. use File::Basename qw(basename);
  12. use File::Spec::Functions qw(curdir splitdir);
  13. my %ignore;
  14. #<<<
  15. @ignore{
  16. 'BEGIN',
  17. 'import',
  18. '__ANON__',
  19. 'AUTOLOAD',
  20. '(~~',
  21. 'ISA',
  22. 'a',
  23. 'b',
  24. 'PREC',
  25. 'ROUND',
  26. } = ();
  27. #>>>
  28. my $dir = shift() // die "usage: $0 sidef/lib\n";
  29. my $name = basename($dir);
  30. if ($name ne 'lib') {
  31. die "error: '$dir' is not a lib directory!";
  32. }
  33. chdir $dir;
  34. find {
  35. no_chdir => 1,
  36. wanted => sub {
  37. /\.pm\z/ && -f && check_file($_);
  38. },
  39. } => curdir();
  40. sub check_file {
  41. my ($file) = @_;
  42. my (undef, @parts) = splitdir($file);
  43. require join('/', @parts);
  44. $parts[-1] =~ s{\.pm\z}{};
  45. my $module = join('::', @parts);
  46. my $sym_table = do {
  47. no strict 'refs';
  48. \%{$module . '::'};
  49. };
  50. while (my ($key, $value) = each %{$sym_table}) {
  51. next if exists $ignore{$key};
  52. if (ref($value) eq '' and not defined(&$value)) {
  53. say "Undefined $module method: <<$key>>";
  54. }
  55. }
  56. }