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

Hi Monks

I'm an ex-C-programmer who finds he has to write just one more program before senior citizenship intervenes. The program must be in Perl (which had only just been invented when I gave it up programming 15 years ago) and will eventually be maintained/extended by other people.

First the good news: programming is like riding a bicycle -- once you have learnt, you won't fall off; Perl is easy. The bad news is that Perl apparently has no equivalent of the C 'const' statement. Constants have to put in variables, which makes the program vulnerable to side-effects unless the programmer can guarantee that the variable can never be assigned a new value, now or in the future, or in the hands of any other person who might pick up the program.

The most common workaround seems to be the "use constant" pragma, which (I think) returns the const value from a sub. However, "use constant" has scoping problems -- can't use it.

What does work for me is Readonly, but the author warns that it is slow.

Two questions:

Replies are listed 'Best First'.
Re: Module Readonly
by hardburn (Abbot) on Apr 23, 2004 at 13:11 UTC

    You are correct in saying constant creates a subroutine. Specifically, it's a sub in the form sub NAME () { 'value' }. The empty prototype is a hint to the compiler that the value can be inlined.

    The reason Readonly is slow is that it relies on a tied interface--in other words, it's an object pretending to be one of Perl's native datatypes.

    One alternative is Scalar::Readonly, which sets an internal flag on a scalar (SvREADONLY). This is the flag set by certain special internal variables by Perl to make them readonly, and Scalar::Readonly lets you fiddle with it. I would be very surprised if this affected performance at all (save for the initial setting of the flag).

    ----
    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Module Readonly
by mutated (Monk) on Apr 23, 2004 at 13:12 UTC
    I am by no means a perl wizard, but I have run into this problem many times. My solution is to stick all my constants into a config file of the format:
    KEY=VALUE KEY=VALUE ...
    if is then really easy to read the values in
    open(F,"config.conf"); my %config = {}' while(<F>) { (my $key,$value) = split /=/; $config{"$key"} = $value; } close(F)
    if you stick this in an object and only reference the object through some function ie: $obj->v('KEY'); then it is unlikely the value will get modified, it is always possible they can modify your "constants" through  $Packagename::config{'key'} but good programing style should dictate that this shouldn't happen.


    daN.
Re: Module Readonly
by calin (Deacon) on Apr 23, 2004 at 16:23 UTC

    Here are a few tricks with read-only scalar thingies. However, you say:

    Constants have to put in variables, which makes the program vulnerable to side-effects unless the programmer can guarantee that the variable can never be assigned a new value, now or in the future, or in the hands of any other person who might pick up the program.

    these tricks don't guarantee anything. There's no magic implementation of Digital Rights Management in Perl :-) (or anywere else for the matter).

    1. Make dynamic scalar variable read-only (idiomatic)

    $ perl *ro = \'readonly_dynamic'; print "=$ro=\n"; defined eval {$ro = 'newval'} or print "Exception: $@"; ^D =readonly_dynamic= Exception: Modification of a read-only value attempted at - line 4.

    2. Make lexical scalar variable read-only (w/ Lexical::Alias)

    $ perl use Lexical::Alias; # 5.8+, see pod my $ro; alias ${\'readonly_lexical'}, $ro; print "=$ro=\n"; defined eval {$ro = 'newval'} or print "Exception: $@"; ^D =readonly_lexical= Exception: Modification of a read-only value attempted at - line 8.

    3. Make hash (or array) element read-only (w/ Array::RefElem)

    $ perl use Array::RefElem qw/av_store hv_store/; hv_store %h, 'ro', ${\'readonly_hashval'}; av_store @a, 5, ${\'readonly_aryelem'}; print "=$h{ro}=$a[5]=\n"; defined eval {$h{ro} = 'newval'} or print "Exception: $@"; defined eval {$a[5] = 'newval'} or print "Exception: $@"; ^D =readonly_hashval=readonly_aryelem= Exception: Modification of a read-only value attempted at - line 7. Exception: Modification of a read-only value attempted at - line 9.
Re: Module Readonly
by Tomte (Priest) on Apr 23, 2004 at 13:06 UTC

    Could you elaborate on the scoping problems use constant has? I didn't find a hint via quick googling or in its pod.

    Just curious...

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

      constant creates a subroutine, which in Perl5 is global to the package. You can fake lexically-scoped subroutines by putting them in a variable (my $sub = sub { . . . };), but that doesn't really help with constants since you can still change the variable that holds the sub ref.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        ? The only mention I see there of scoping is a mention that "use constant" constants are global, not lexically scoped. How is that a problem?
Re: Module Readonly
by Zaxo (Archbishop) on Apr 24, 2004 at 04:05 UTC

    You can combine calin's glob assignment with local to get dynamic scoping of such constants,

    { local *foo = \"foo"; bar() } sub bar { print "${foo}bar$/" } bar(); __END__
    which prints,
    foobar bar
    unless you have warnings on, which will complain of undefined $foo in the second call of sub bar.

    After Compline,
    Zaxo

Re: Module Readonly
by pizza_milkshake (Monk) on Apr 23, 2004 at 15:11 UTC
Re: Module Readonly ( *foo = \'bar' )
by Anonymous Monk on May 30, 2013 at 07:23 UTC
    Attribute::Constant/Data::Lock claims to be faster than *glob = \'constant', both of which are lots faster than Readonly
    $ 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.