I don't see how that works. The code still runs at run-time, and the use statement still runs at compile time. Using $x instead of $x == 1 in the ternary eliminates a "Use of uninitialized value $x in numeric eq (==) ..." warning, but that's all.
>perl -wMstrict -le "my $x; BEGIN { my $m = `uname -m`; print qq{m '$m'} if $m =~ /x86/; $x = 1 if $m =~ /x86/; use constant FOO => ($x ? 'true' : 'false'); } print FOO; print qq{x '$x'}; " m 'x86 ' false x '1'
Update: This seems to work:
>perl -wMstrict -le "BEGIN { use constant X => (`uname -m` =~ /x86/) ? 1 : 0; use constant FOO => (X ? 'true' : 'false'); } print FOO; print q{X '}, X, q{'}; " true X '1'
Update: Oh, wait... I didn't catch the fact that the use constant statement is outside the BEGIN block! tobyink's code works as advertised (per tobyink's reply which I didn't see until after I had posted this update). Evidently it helps if you test the actual code. Nevermind.
>perl -wMstrict -le "my $x; BEGIN { my $m = `uname -m`; print qq{m '$m'} if $m =~ /x86/; $x = 1 if $m =~ /x86/; } use constant FOO => ($x ? 'true' : 'false'); print FOO; print qq{x '$x'}; " m 'x86 ' true x '1'
In reply to Re^4: Unable to assign value to constants using ternary operator
by AnomalousMonk
in thread Unable to assign value to constants using ternary operator
by sathya_myl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |