This is one I developed seperately with more stuff in it, also allowing constant hashes and arrays (much like tuples in python as I understand them). It also uses a different and IMHO cleaner syntax for creating the constants:
Const my $foo => 42;

Here's the sample program:

#!/usr/bin/perl -w use strict; use Const qw/Const ConstHash ConstArray/; Const my $FOO => 42; # tie my $FOO, 'Const', 42; print "$FOO\n"; # prints "42" $FOO = 43; # croaks for attempted change $FOO++; # same ConstHash my %BOO => { foo => 'blah', moo => 41, 2 => 'no' }; # tie my %BOO, 'ConstHash', { foo => 'blah', moo => 41, 2 => 'no' }; print "$BOO{foo} $BOO{2}\n"; # prints "blah no" $BOO{moo}++; # croaks for attempted change %BOO = (); # same print "$BOO{Foo}\n"; # croaks for non-existant element ConstArray my @MOO => [ qw/foo 2 five seven/ ]; # tie my @MOO, 'ConstArray', [ qw/foo 2 five seven ]; print "$MOO[0] $MOO[3]\n"; # prints "foo seven" $MOO[1]++; # croaks for attemped change $MOO[5] = 2; # same splice(@MOO,1,1,4); # same again print "$MOO[10]\n"; # croaks for non-existant element
And the implementation in Const.pm (click Read More)
use 5.006; use strict; use Exporter (); use Carp (); ####################################################### package Const; # allows export of function to faciliate constant usage ####################################################### our @EXPORT_OK = qw/Const ConstHash ConstArray/; our @ISA = qw/Exporter/; sub Const ($$) { tie $_[0], 'ConstScalar', $_[1] } sub ConstHash (\%$) { tie %{ $_[0] }, 'ConstHash', $_[1]; } sub ConstArray (\@$) { tie @{ $_[0] }, 'ConstArray', $_[1]; } ############################################# package ConstScalar; # scalars set on tying, altering causes croak ############################################# use Tie::Scalar (); our @ISA = qw/Tie::StdScalar/; sub STORE { Carp::croak "fatal: Const cannot be altered" } ########################################### package ConstHash; # hashes which are fully set on tying, # altering, or accessing unset keys croaks ########################################### use Tie::Hash (); our @ISA = qw/Tie::StdHash/; sub TIEHASH { bless { %{$_[1]} }, $_[0] } # shallow clone sub STORE { Carp::croak "fatal: ConstHash cannot be altered" } sub DELETE { Carp::croak "fatal: ConstHash cannot be altered" } sub CLEAR { Carp::croak "fatal: ConstHash cannot be altered" } sub FETCH { if (exists $_[0]->{$_[1]}) { return $_[0]->{$_[1]} } else { Carp::croak "fatal: key '$_[1]' doesn't exist in this C +onstHash" } } ############################################# package ConstArray; # Arrays which are fully set on tying, # altering or accessing unset indexes croaks ############################################# use Tie::Array (); our @ISA = qw/Tie::StdArray/; sub TIEARRAY { bless [ @{$_[1]} ], $_[0] } # shallow clone BEGIN{ #lazy shortcut for (qw/STORESIZE STORE CLEAR POP PUSH SHIFT UNSHIFT DELETE SP +LICE/) { eval "sub $_ { Carp::croak 'fatal: ConstArray cannot b +e altered' }"; } } sub FETCH { if ($_[1] > $#{$_[0]}) { Carp::croak "fatal: array element $_[ +1] does no t exist in this ConstArray" } $_[0]->[$_[1]]; } ############################################# 42;

In reply to Re: Re: Use constant vs. hashes by repson
in thread Use constant vs. hashes by legLess

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.