Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Compile Time Checking of Subroutine Calls is Possible After All

by sierrathedog04 (Hermit)
on Jul 12, 2001 at 05:14 UTC ( #95899=perlquestion: print w/replies, xml ) Need Help??
sierrathedog04 has asked for the wisdom of the Perl Monks concerning the following question:

Several weeks ago I asked my SOPW colleagues whether it would be possible for perl to check at compile time whether the named subroutines which I called actually existed. Several monks informed me that it was difficulty for perl to do so.

Thus, while mistyping a variable name always produces a compile-time error (assuming 'use strict' is on) mistyping a subroutine name only produced an error if at run-time the call to the non-existent subroutine actually occurred.

A recent posting by Tilly made me realize how to get compile time checking of my subroutine calls. Use anonymous subroutines.

It is all so easy. Here is a code snippet that shows how:

use strict; my $flag = shift; my $hello = (sub { return "hello by reference\n";}); print $xello->() if $flag;
The above code fails at compile time because I mistyped the name of the subroutine reference. Call your subroutines using the above type of subroutine reference instead of subroutine name and you will never search for another typo in your subroutine calls again.

Named subroutines. Who needs 'em?

Replies are listed 'Best First'.
Re: Compile Time Checking of Subroutine Calls is Possible After All
by bikeNomad (Priest) on Jul 12, 2001 at 06:11 UTC
    Except that you don't get proper OO lookup/inheritance. I suppose if all you're doing is regular procedural stuff, it works OK. You could try B::Lint, which for this program:

    use strict; my $flag = shift; sub hello { return "hello by reference\n" } print xello() if $flag;

    prints this diagnostic:

    $ perl -MO=Lint,all Undefined subroutine xello called at line 4 syntax OK

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://95899]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2018-10-18 11:04 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (101 votes). Check out past polls.