Jan Dubois <> wrote:
> On Wed, 07 Dec 2005, Jan Dubois wrote:
>> Or you can define a normal subroutine:
>> 
>>     sub DATA_COLS () { $os eq 'X') ? 6 : 7 }
>> 
>> assuming $os will not change during the lifetime of your program.
> 
> I just realize that I should have emphasized the importance of
> specifying the empty prototype for the "constant" function.  This
> makes a big difference when you use it without parentheses:  
> 
>     my $foo = DATA_COLS + 1;
> 
> will be compiled as
> 
>     my $foo = DATA_COLS() + 1;
> 
> If you didn't specify the prototype:
> 
>     sub DATA_COLS { $os eq 'X') ? 6 : 7 }
> 
> then it would be compiled as
> 
>     my $foo = DATA_COLS(+ 1);
> 
> effectively ignoring the "+ 1" part.
> 
> Demo:
> 
>     C:\>perl -we"sub foo {5}; print foo + 1"
>     5
>     C:\>perl -we"sub foo () {5}; print foo + 1"
>     6

I was going to do the "there are few occasions when a subroutine
prototype is necessary, but this is one of them" speech, but you beat me
to it.

Just to add that the original request sounded more like a readonly
variable than a constant. I which case the Readonly module (and perhaps
it's friend Readonly::XS) might be worth investigating.

HTH

-- 
Brian Raven



=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the 
intended addressee(s). Unauthorised reproduction, disclosure, modification, 
and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message do not 
necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement 
destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. 
Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail 
vous parvient par erreur, nous vous prions de bien vouloir prevenir 
l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre 
systeme. Le contenu de ce message electronique ne represente pas necessairement 
la position ou le point de vue d'Atos Euronext Market Solutions.


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to