I am currently writing an perl OO based app, and I have written a small module which contains a number of constants, needed by separate disparate modules. This module looks something like
package App::Constants; # NOT its real name use constant MAX_PRIORITY => 10000; use constant DEFAULT_WIDTH => 7; use constant MSG_DATA => [ { # front ID_LT => 480, SEQ_LT => 481, }, { # back ID_LT => 810, SEQ_LT => 811, }, ]; use base qw(Exporter); our @EXPORT_OK = qw(MAX_PRIORITY DEFAULT_WIDTH MSG_DATA); our %EXPORT_TAGS = (default => \@EXPORT_OK); 1;
I need to loop over the hashes in MSG_DATA at one point in my code. And to do this , my code looks like
package App::Document; use App::Constants qw(default); sub apply { foreach my $msg (@{&MSG_DATA}) { some_func($msg->{ID_LT}); } } 1;
Note the little & before the constant MSG_DATA - it is required under 'use strict;', otherwise you get an error about ambiguous usage or symbolic reference or any number of problems. You can also get the 'right' actions if you use
@{+MSG_DATA}; # syntactic separation with unary plus @{MSG_DATA()}; # function that returns reference to array
My concern is that the &MSG_DATA and MSG_DATA() usages betray the fact that I know the current implementation of the constant.pm module uses subroutines internally - and this is an implementation detail I would prefer not to exploit, as future change will break my code (unless that change is backwards compatible, which I dont want to bet on either). And the +MSG_DATA usage is somewhat obscure, leading to possible maintenance problems in the future for whoever looks after this after me.

So the question is, is there a better way to use a constant data structure like this, that protects against future implementation changes and is obvious to a moderately knowledgable perl'er.


+++++++++++++++++
/#!/usr/bin/perl use warnings; use strict; use brain;

In reply to looping over constant data structure references by leriksen

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.