Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks.

This is probably a fairly elementary question but in attempting to create a constant in perl, e.g:
*myVar = \3.14159;

The reference to the numeric value is stored in the symbol table (since it is not a lexical variable). How do you suggest I display its value under use strict;?

print ${*myVar}; # or... print ${*myVar{SCALAR}}; # or... print $main::myVar; # or... no strict 'vars'; print $myVar;


Thanks.

Replies are listed 'Best First'.
Re: Typeglobs and constant variables
by kcott (Archbishop) on May 30, 2013 at 06:35 UTC

    There's a built in pragma called constant which I'd probably reach for before trying to do something clever with typeglobs:

    $ perl -Mstrict -Mwarnings -le ' use constant MYVAR => 3.14159; print MYVAR; use constant PI => 4 * atan2(1, 1); print PI; ' 3.14159 3.14159265358979

    However, if you're stuck with the typeglob, I'd take a pragmatic approach and pick the one which involves the least typing (i.e. ${*myVar}).

    $ perl -Mstrict -Mwarnings -le ' *myVar = \3.14159; print ${*myVar}; print ${*myVar{SCALAR}}; print $main::myVar; ' 3.14159 3.14159 3.14159

    -- Ken

Re: Typeglobs and constant variables
by AnomalousMonk (Archbishop) on May 30, 2013 at 06:56 UTC

    If you don't want to declare with our or use constant (maybe also take a look at Readonly, but that seems to exact a speed penalty (Update: see thread Module Readonly, esp. the most recent reply)),  $::myVar is a little bit less typing than  $main::myVar (well, at least as long as you're in the  main package).

    >perl -wMstrict -le "*myVar = \3.14159; print $::myVar; " 3.14159
        My preference would be for Const::Fast. My reasons for this are in "Re: Use of Constants"

        One caveat -- beside the build problem -- of that module that it fails to mention, is that its "constants" aren't (compile-time) constants, but rather runtime variables that cannot be varied [sic], and thus they do not trigger Perl's compile-time optimisations.

        For me, that issue alone negate the purpose of using constants almost completely. Combined with the other two issues render it a non starter.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Typeglobs and constant variables
by choroba (Cardinal) on May 30, 2013 at 06:27 UTC
    What about
    *var = \3.14159; our $var; print $var;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Typeglobs and constant variables (read-only math PI constant)
by Anonymous Monk on May 30, 2013 at 07:35 UTC

    Technically in perl-speak, that is the typeglob way to create a read-only variable, not a constant

    This is a constant  sub var() { 42 } print var;

    This is a read-only

    $ perl -Mstrict -le " our $var; *var= \3.14159; print $var; $var++ " 3.14159 Modification of a read-only value attempted at -e line 1.

    This is fast read-only

    $ perl -le " use Attribute::Constant; my $pi : Constant( 3.14159265358 +9793 ); print $pi; $pi++" 3.14159265358979 Modification of a read-only value attempted at -e line 1.

    OTOH

    $ perl -Mbignum=PI -le " print PI() " 3.141592653589793238462643383279502884197