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

    You should note that your proposed solution (using Perl in a C-like fashion with a main() function) does not actually aid the compiler in any way.

    The compiler compiles all tokens in sequence from the first to the last. Certain constructs, such as 'BEGIN{...}' or 'use ...;' are executed as soon as they finish compiling, and cause the compiler to recurse, while other constructs such as "sub NAME ..." generate symbol table entries as soon as they finish compiling.

    Your code defines the symbol 'main' (perhaps confusing, given that the fully qualified name is 'main::main'), uses the symbols 'get_input' and 'say_hello'. Then defines the symbol 'get_input' and 'say_hello'. At the very end, the compiler terminates and the execution phase begins. The first executable statement is 'main()'.

    Definately, choosing to always invoke subroutines using () over using list operator syntax, will avoid this particular problem. The problem that it does not avoid, is the situation where a subroutine is invoked, but later prototyped:

    a(@a); sub a ( \@ ) { my($array_reference) = @_; }

    If -w is not used, Perl is perfectly happy to execute the above code generating undesirable results ($array_reference is assigned the first value in @a). If -w is specified, Perl generates the warning message:

    main::a() called too early to check prototype at ...

    Moral of the story: If you use prototypes at all, make sure to always pre-declare your subroutines using "sub NAME (PROTOTYPE);". However, if you have read about the pitfalls regarding prototypes, you may decide that it is reasonable to completely avoid using prototypes at all, in which case, always using () when invoking subroutines will eliminate the problem described by the original poster (list operator not recognized).

    Secondary moral of the story: -w can be extremely useful.

Re: Re: Forward-referenceing subs
by ihb (Deacon) on Jan 12, 2003 at 13:31 UTC
    Yes, you can use the '&' symbol to call your subroutines (ie: &foo;), but IMO it makes things uglier.

    Not only does it make it uglier, it also doesn't do the same thing. The issue is covered in perlfaq7, "What's the difference between calling a function as &foo and foo()?". I pointed out the very same thing in Re: Hash of Hash of Listed subroutines but then in the context of dereferencing subroutines. For some reason, it's much more common to drop the parenthesis when dereferencing than when making a regular subroutine call.

    ihb