[Melbourne-pm] How to create data structures with 'default values'
Alec Clews
alec.clews at gmail.com
Tue Nov 18 02:08:19 PST 2008
Many thanks for the replies. I decided in the end to adopt Shlomi's
suggestions as it fitted my current data structures and provided the
ability to have extra levels of defaults. Here is the sample
implementation
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %globalDefaults = (
key1 => 'defValue1',
key2 => 'defValue2',
key3 => 'defValue3',
);
my %localDefaults = (
key2 => 'localValue2',
key3 => 'localValue3',
);
my @envSettings = ( # Array -- settings for each config file
{ file_in => "myTemplateFile",
file_out => "outputFile",
#templateValues => {}, #Filled in later
templateSettings => {
key3 => "fileValue3",
key4 => "fileValue4",
}
},
{ file_in => "myTemplateFile2",
file_out => "outputFile2",
#templateValues => {}, #Filled in later
templateSettings => {
key3 => "2fileValue3",
key4 => "2fileValue4",
}
},
);
foreach my $fileTemplateSettings (@envSettings) {
%{ $fileTemplateSettings->{'templateValues'} } = (
%globalDefaults, %localDefaults,
%{ $fileTemplateSettings->{'templateSettings'} }
);
}
foreach my $fileTemplateSettings (@envSettings) {
print Dumper %{ $fileTemplateSettings->{'templateValues'} };
print "\n";
}
-
Alec Clews
Personal <alec.clews at gmail.com> Melbourne, Australia.
Jabber: alecclews at jabber.org.au PGPKey ID: 0x9BBBFC7C
Blog http://alecthegeek.wordpress.com/
On Mon, 2008-11-17 at 20:30 +1100, Alec Clews wrote:
> G'Day Perl Bunnies,
>
> I have created a hash structure with a set of environment specific keys
> and values.
>
> * There is a different hash for each of the environments.
>
> * Many of the values are the same (default) across all the
> environments.
>
> What is the easy way of setting up the defaults once and then have each
> environment overwrite the default values with specific settings?
>
> Currently I have a hash of defaults. However it's a pain to define each
> specific value or assign the default value for every single key in every
> single environment.
>
> Thanks.
>
>
> -
> Alec Clews <alec.clews at voga.com.au> Principal Consultant
> Jabber: alecclews at jabber.org.au Voga Consulting Services
> skype: alecclews Voice: +61(425)770-886 http://www.voga.com.au/
> blog:http://alecthegeek.wordpress.com/ Melbourne, Australia
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20081118/a724d686/attachment.bin>
More information about the Melbourne-pm
mailing list