in reply to looping over constant data structure references

Constants using the constant pragma need not be scalars, they can be arrays lists too:
use Data::Dumper; use constant MSG_DATA => { foo => 1 }, { bar => 2 }, { baz => 3 }; for my $msg (MSG_DATA) { print Dumper $msg; }
Use [ MSG_DATA ] elsewhere in the code if you need to use the list as an reference.

Incidentally, this feature is the source of many accidental bugs with constant:

use constant FOO => "xyz", BAR => "def", BAZ => "abc"; # FOO returns qw/xyz BAR def BAZ abc/ # BAR and BAZ aren't defined as constants
The programmer probably meant to declare three constants, not one:
use constant { FOO => "xyz", BAR => "def", BAZ => "abc" };

blokhead

Replies are listed 'Best First'.
Re: Re: looping over constant data structure references
by leriksen (Curate) on Nov 11, 2003 at 05:09 UTC
    thanks , I'll ponder on that one - it does seem better - a list matches the constant idea better - contents of constant references are mutable, so removing the []'s removes that level of writable-ness, if that's the right term.

    I'd worry about the [MSG_DATA] though - this copies MSG_DATA and returns a reference to the copy - this may or may not be what you want.
    +++++++++++++++++
    #!/usr/bin/perl use warnings; use strict; use brain;