Here's a sketch of what this should look like. It might even work, as-is.

your_script.pl

use Strict::Subs; foo(); # The subroutine main::foo might be called but it doesn't exist + yet.

Strict::Subs.pm

package Strict::Subs; use strict; use warnings; use Exporter; use B::Utils qw( all_roots anon_subs ); use vars qw( %Violations @ISA @EXPORT $VERSION ); BEGIN { $VERSION = '0.01'; @ISA = 'Exporter'; @EXPORT = 'strict_subs'; 1; } sub import { eval q[ CHECK { # Provide a named way to trigger this apply_strict_subs(); 1; } ]; } sub strict_subs () { 'strict_subs()'; } sub apply_strict_subs { local %Violations; # All named subroutines. { my %named_subs = all_roots(); _strict_sub( $_ ) for values %named_subs; } # All anonymous subroutines _strict_sub( $_->{'root'} ) for anon_subs(); 1; } sub _strict_sub { my $root = shift; walkoptree_filtered( $root, \ &_find_strict_sub_invocation, \ &_apply_strict_subs ); 1; } sub _find_strict_sub_invocation { my $op = shift; opgrep( { name => 'gv' }, $op ) and do { my $gv = $op->sv; ( $gv->NAME eq 'strict_subs' and $gv->STASH->NAME eq 'Strict' ) } } sub _apply_strict_subs { my $op = shift; walkoptree_filtered( $_, \ &_find_subroutine_calls, \ &_validate_subroutine_existance ) for $op->younger_siblings(); 1; } sub _find_subrountine_calls { opgrep( { name => 'gv' next => { name => 'entersub ' } } ); } sub _validate_subroutine_existance { my $op_gv = shift; my $gv = $op_gv->sv; my $name = $gv->STASH->NAME . '::' . $gv->NAME; no strict 'refs'; *{$name}{'CODE'} or warn "The subroutine $name might be called but it doesn't e +xist yet.\n" } package B::Utils; sub younger_siblings { my $op = shift; my @siblings; for ( my $sibling = $op->sibling; $sibling->oldname ne 'null'; $op = $sibling ) { push @siblings, $sibling; } @siblings; }

In reply to Re^2: Another Story (Detect undefined subroutines) by diotalevi
in thread Detect undefined subroutines by Sprad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.