Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found