This is a practical way to write code, but if you have a large project you end up with some very large subs and a very large file full of code. This becomes difficult to work on. One technique for breaking up the code a little bit is to use an object:use Data::Dumper; my %data; my @projects= qw ( dogs cats birds horses ); my $langs= { it => 'Italian', es => 'Spanish', en => 'English' }; my @targets= qw ( images data links other ); foreach my $project (@projects) { foreach my $lang_abbrev (keys %$langs) { foreach my $target (@targets) { $data{$project}{$lang_abbrev}{$target}= $lang_abbrev.$project.'/'.$target.'.tar.bz2'; } } } print Dumper(\%data);
So far the object hasn't helped very much. Having one large object for your whole program doesn't do much good. Better to build the big object out of smaller objects:use Data::Dumper; my $data= new BigData(); print Dumper($data); package BigData; sub new { my $class= shift; my $data= {}; my @projects= qw ( dogs cats birds horses ); my $langs= { it => 'Italian', es => 'Spanish', en => 'English' }; my @targets= qw ( images data links other ); foreach my $project (@projects) { foreach my $lang_abbrev (keys %$langs) { foreach my $target (@targets) { $data->{$project}{$lang_abbrev}{$target}= $lang_abbrev.$project.'/'.$target.'.tar.bz2'; } } } bless $data, $class; return $data; } 1;
This type of code also gets complicated. You would probably end up making more objects. Each object gets its own module in its own file. If you do it right, that makes it easier to maintain. The key is to keep your subroutines and files from getting too large.use Data::Dumper; my $data= new BigData(); print Dumper($data); package BigData; sub new { my $class= shift; my $data= {}; my @projects= qw ( dogs cats birds horses ); my @langs; push @langs, new Lang( { abbrev => 'it', name => 'Italian' } ); push @langs, new Lang( { abbrev => 'es', name => 'Spanish' } ); push @langs, new Lang( { abbrev => 'en', name => 'English' } ); my @targets= qw ( images data links other ); foreach my $project (@projects) { foreach my $lang (@langs) { foreach my $target (@targets) { $data->{$project}{$lang->get_abbrev()}{$target}= $lang->get_abbrev().$project.'/'. $target.'.tar.bz2'; } } } bless $data, $class; return $data; } 1; package Lang; sub new { my $class= shift; my $lang= {}; my ($props)= @_; foreach my $key (keys %$props) { $lang->{$key}= $props->{$key}; } bless $lang, $class; return $lang; } sub get_abbrev { my $lang= shift; return $lang->{abbrev}; } 1;
Here are some tips:
In reply to Re: Dating a Structure
by toma
in thread Dating a Structure
by hacker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |