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?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.