Short answer: no.

Long answer: perl doesn't know at compile time whether the typeglob CODE slot it allocates at compile time will be actually filled with code at run time.

#!/usr/bin/perl # use warnings; use strict; use 5.008; sub monk($) {} sub go() { mink(123); } BEGIN { print "$_\n" for grep { /^m/ } sort keys %main:: } go(); __END__ main:: mink monk Undefined subroutine &main::mink called at sub.pl line 5.

Mentioning a subroutine somewhere in the code has (as far as typeglob allocation goes) a similar effect as subroutine forward declarations:

#!/usr/bin/perl # use warnings; use strict; use 5.008; sub mink; # sub mink still undefined, but typeglob allocated. sub monk($) {} sub go() { mink(123); } BEGIN { print "$_\n" for grep { /^m/ } sort keys %main:: } go(); __END__ main:: mink monk Undefined subroutine &main::mink called at sub.pl line 6.

AFAIK there's no pragma which enforces allocating the code reference *) in the CODE slot along with typeglob allocation.

*) Actually it is not just the code reference (which is what you get when you look into the CODE slot) but something deeper in perl's data structures. That is why

use strict; $SIG{CHLD} = \&mywait; # value in the %SIG hash is an empty code ref

compiles without problem, even if the sub mywait { } never gets defined, and the coderef therefor is never populated with code proper.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: Possible to catch undefined sub at "compile" time? by shmem
in thread Possible to catch undefined sub at "compile" time? by Anonymous Monk

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.