I think a good reference for this is the book 'Intermediate Perl'. Here is some code without using any objects:
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);
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= 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;
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; 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;
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.

Here are some tips:

  1. Whenever you have something that looks like a large case statement, you can somehow create a class to replace it.
  2. Translate the insides of deeply nested loops into subs.
  3. It isn't always obvious how to structure your code or your packages. Leave time for trying different approaches.
  4. Try using modules from cpan instead of writing your own code. For example maybe Locale::Language would be useful.
It should work perfectly the first time! - toma

In reply to Re: Dating a Structure by toma
in thread Dating a Structure by hacker

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.