in reply to Forward-referenceing subs
The best way to avoid this type of 'accident' is to call your subroutines with a pair of parentheses. Yes, you can use the '&' symbol to call your subroutines (ie: &foo;), but IMO it makes things uglier. IIRC, the & symbol is a depreceated method of calling a subroutine. Yes, it's easy to see that you are calling a custom (not a core) subroutine, but it clutters code.
Because everybody here cares so much, here's my favorite (and long-time) way of writing up scripts. Personally, I always put my subroutines at the top, write up a main() sub (like C/C++ which I have never used), and call main() at the bottom. I always use parentheses to call my subs, whether I am passing arguments or not.
#!perl -w use strict; sub main { print "Welcome to my program!\n"; my ($name, $email) = ( get_input('Your Name'), get_input('Your Email Address') ); say_hello($name, $email); } sub get_input { my $msg = shift; print $msg, ': '; chomp( $_ = <STDIN> ); return $_; } sub say_hello { my ($name, $email) = @_; print "Hello $name, I have sent mail to $email!\n"; } main();
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Forward-referenceing subs
by MarkM (Curate) on Jan 12, 2003 at 10:42 UTC | |
Re: Re: Forward-referenceing subs
by ihb (Deacon) on Jan 12, 2003 at 13:31 UTC |