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

Cheers,
-Jan


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

Reply via email to