[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Subroutines



Terje Bless <link@tss.no> writes:
>Tim Judd writes:
>[SNIP]
>>Sample snippet from code:
>>-----
>>if ($UserPassword eq $Data[1]) {
>>  print "Password accepted.\n";
>>  MainMenu
>>  } else {
>> [...]
>> sub MainMenu {}

>Try :
>  &MainMenu;
># ^________^  4&4 may not be neccesary, but the 4;4 is.

Other way around, in fact :-) The ";" is optional, but the & isn't, in this
context. The problem is that at the point of the call, the compiler doesn't
know yet that MainMenu is a procedure name, so it gets interpreted as a
"Bareword", i.e., the string "MainMenu" and thus the script ends without
calling the subroutine. The soplution is to either use & or declare the
subroutine at the beginning of the script.

Subroutines may be declared with either:

use subs qw(MainMenu);

or

sub MainMenu;

All of this is strictly Perl5. In Perl4, & is mandatory so there is never an
order problem.
Matthias

-----
Matthias Neeracher   <neeri@iis.ee.ethz.ch>   http://www.iis.ee.ethz.ch/~neeri
   "One fine day in my odd past..." -- Pixies, _Planet of Sound_