Perl Programming Page 2 - Subroutines in Perl |
Don’t give subroutines the same names as built-in functions. If you declare a subroutine with the same name as a built-in function, subsequent invocations of that name will still call the builtin...except when occasionally they don’t. For example: sub lock { sub link { lock($file); # Calls 'lock' subroutine; built-in 'lock' hidden Perl considers some of its builtins (like link) to be “more built-in” than others (likelock), and chooses accordingly whether to call your subroutine of the same name. If the builtin is “strongly built-in”, an ambiguous call will invoke it, in preference to any subroutine of the same name. On the other hand, if the builtin is “weakly built-in”, an ambiguous call will invoke the subroutine of the same name instead. Even if these subroutines did always work as expected, it’s simply too hard to maintain code where the program-specific subroutines and the language’s keywords overlap: sub crypt { return "You're in the tomb of @_\n" } print crypt( qw( Vlad Tsepes ) ); # Subroutine or builtin? for my $reward (qw( treasure danger) ) { print hex('the Demon'); # Subroutine or builtin? There is an inexhaustible supply of subroutine names available; names that are more descriptive and unambiguous. Use them: sub in_crypt { return "You're in the tomb of @_\n" } print in_crypt( qw( Vlad Tsepes ) ); for my $reward (qw( treasure danger )) { print hex_upon('the Demon');
blog comments powered by Disqus |