How can I smoke out undefined subroutines?

后端 未结 3 1349
情深已故
情深已故 2021-01-04 00:58

I want to scan a code base to identify all instances of undefined subroutines that are not presently reachable.

As an example:

use strict;
use warnin         


        
3条回答
  •  滥情空心
    2021-01-04 01:54

    To find calls to subs that aren't defined at compile time, you can use B::Lint as follows:

    a.pl:

    use List::Util qw( min );
    
    sub defined_sub { }
    sub defined_later;
    sub undefined_sub;
    
    defined_sub();
    defined_later();
    undefined_sub();
    undeclared_sub();
    min();
    max();              # XXX Didn't import
    List::Util::max();
    List::Util::mac();  # XXX Typo!
    
    sub defined_later { }
    

    Test:

    $ perl -MO=Lint,undefined-subs a.pl
    Undefined subroutine 'undefined_sub' called at a.pl line 9
    Nonexistent subroutine 'undeclared_sub' called at a.pl line 10
    Nonexistent subroutine 'max' called at a.pl line 12
    Nonexistent subroutine 'List::Util::mac' called at a.pl line 14
    a.pl syntax OK
    

    Note that this is just for sub calls. Method calls (such as Class->method and method Class) aren't checked. But you are asking about sub calls.

    Note that foo $x is a valid method call (using the indirect method call syntax) meaning $x->foo if foo isn't a valid function or sub, so B::Lint won't catch that. But it will catch foo($x).

提交回复
热议问题