in reply to Possible to catch undefined sub at "compile" time?

One possible solution to this would be to create test scripts (e.g. using Test::More) for the module in question:
package foo; use warnings; use strict; sub monk { return 42; } sub go { return mink(123); } 1; __END__
use strict; use warnings; use Test::More tests => 3; use lib qw (.); BEGIN { use_ok('foo'); } # ------ monk ------ cmp_ok(foo::monk(), q{==}, 42, q{Expect monk to return 42}); # ------ go ------ cmp_ok(foo::go(), q{==}, 42, q{Expect go to return 42}); __END__
Running the test yields:
$ prove foo.t foo....ok 1/3Undefined subroutine &foo::mink called at foo.pm line 8. # Looks like you planned 3 tests but only ran 2. # Looks like your test died just after 2. foo....dubious Test returned status 255 (wstat 65280, 0xff00) DIED. FAILED test 3 Failed 1/3 tests, 66.67% okay Failed Test Stat Wstat Total Fail List of Failed ---------------------------------------------------------------------- +--------- foo.t 255 65280 3 2 3 Failed 1/1 test scripts. 1/3 subtests failed. Files=1, Tests=3, 1 wallclock secs ( 0.04 cusr + 0.00 csys = 0.04 C +PU) Failed 1/1 test programs. 1/3 subtests failed.
--
Andreas

Replies are listed 'Best First'.
Re^2: Possible to catch undefined sub at "compile" time?
by Anonymous Monk on Oct 10, 2007 at 11:45 UTC
    Andreas, thanks, but this is catching the problem at runtime. Ideally what I would like is a warning/error when referencing a sub which is not already defined. I'm sure this isn't possible in the general case, but perhaps it is possible for code similar to my simple example?
    --Ben

      The problem is, it may require running the script to determine if a function really isn't defined. You may have two scripts that declare functions into the same namespace, use AUTOLOAD, or generate functions within the script (possibly with input from an external source)

      foreach my $function (<DATA>) { no strict refs; chomp $function; *$function = sub { print "$function : @_\n"; }; } mink(12); go('somewhere'); monk(8); __DATA__ mink go