Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Compile Time Checking of Subroutine Calls is Possible After All

by sierrathedog04 (Hermit)
on Jul 12, 2001 at 05:14 UTC ( [id://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 blint.pl Undefined subroutine xello called at blint.pl line 4 blint.pl syntax OK

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-25 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found