leriksen has asked for the wisdom of the Perl Monks concerning the following question:
I need to loop over the hashes in MSG_DATA at one point in my code. And to do this , my code looks likepackage 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;
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 usepackage App::Document; use App::Constants qw(default); sub apply { foreach my $msg (@{&MSG_DATA}) { some_func($msg->{ID_LT}); } } 1;
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.@{+MSG_DATA}; # syntactic separation with unary plus @{MSG_DATA()}; # function that returns reference to array
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: looping over constant data structure references
by blokhead (Monsignor) on Nov 11, 2003 at 04:53 UTC | |
by leriksen (Curate) on Nov 11, 2003 at 05:09 UTC | |
|
Re: looping over constant data structure references
by Aristotle (Chancellor) on Nov 11, 2003 at 06:45 UTC |