[EMAIL PROTECTED] wrote:
> Wizards & Wise Ones,
>
> Is there a way to have a constant take on different values?  I've
> tried the code below, and all I get as a value for DATA_COLS is seven.
> Always seven.
>
>         use Getopt::Std;
>
>         my %opts;
>
>         getopts( 'o:', \%opts );
>
>         my $os = $opts{'o'};
>
>         use constant (DATA_COLS => (($os eq 'X') ? 6 : 7));
>
> Okay, this is apparently wrong.  Is there a right way, some way I can
> set DATA_COLS to different values based on input?  I can't use the
> Readonly module to create a "non-changeable variable," it's not
> installed in the target systems, just development (beats me, too, but
> that's how it is). Or must I give up the idea of having a constant and
> just let DATA_COLS become the (vanilla) variable $DATA_COLS?

You either have to put *all* the code that sets up $os into a BEGIN block
in front of your "use constant" statement.

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.

Constants are really no more than subroutines that return a constant value.
The compiler will then inline those subroutines instead of generating
function calls.  So you lose a little bit of performance by calling
a real sub at runtime, but I doubt you'll actually notice.

Cheers,
-Jan



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

Reply via email to