in reply to Detecting undefined subroutines at compile time

you can use a perl-parser to find all calls and eval{} them.

E.g. I have a patch of B::Deparse which extracts all such calls, but this opens new problems since compiling perl involves executing BEGIN{} blocks.

IIRC there are also moduls in CORE to parse packages like etags and ctags do.¹

Cheers Rolf

UPDATES:

1) I was thinking of B::Xref

~:/tmp$ cat tst.pl foo(); sub foo { print } foo(); ~:/tmp$ perl -MO=Xref,-d tst.pl File tst.pl Subroutine (main) Package main &foo &1, &4 tst.pl syntax OK ~:/tmp$

so you can find all calls, now you just have to check the STASHes in a BEGIN Block.

HTH!

Replies are listed 'Best First'.
Re^2: Detecting undefined subroutines at compile time
by Corion (Patriarch) on May 02, 2011 at 18:10 UTC

    In absence of AUTOLOAD, you don't even need to eval the calls. You can just use defined &func to check whether a subroutine was defined. This will supposedly even see through forward declarations of

    sub foo;

    You still need to run the BEGIN blocks of all use statements at least.

      just found that B::Xref lists sub-calls and(!) sub-definitions:
      perl -MO=Xref tst.pl |grep foo tst.pl syntax OK &foo s1 &foo &1, &4 Subroutine foo

      so just needing to parse¹ and compare the output.

      But I agree that the dynamic nature of Perl makes such static parsing look strange.

      Cheers Rolf

      1) especially the raw output could be easily processed:

      $ perl -MO=Xref,-r tst.pl |grep foo tst.pl (definitions) 1 main & foo + subdef tst.pl syntax OK tst.pl (main) 1 main & foo + subused tst.pl (main) 4 main & foo + subused tst.pl foo 3 main $ _ + used $ cat tst.pl foo(); sub foo { print } foo();