From: Rob Richardson <[EMAIL PROTECTED]>
> My biggest complaint about Perl is the non-intuitive data structures.
> The train schedule program I've been using this list as a resource for
> features a Schedule class that has a collection of ScheduleDay
> objects, each of which has a collection of Train objects, each of
> which has a couple of attributes and an array of crew members.  In C++
> and Visual Basic, both of which I am employed to write programs in,
> this is very easy to write, and perhaps more important, it is very
> easy for somebody else to come along and read and understand what I
> did.  I have managed to build these structures in Perl, but it's hard
> for anybody else to understand because the code is so aggressively
> un-self-documenting. Does PHP have data structures that are defined in
> a manner similar to C++ or VB?  If so, it would probably be worth my
> while to learn it.

Maybe you should stop thinking in C++ when you are programming in 
Perl :-)


package Schedule;

sub new {
        my $class = shift;
        my ($this, $that) = @_;
        my $self = {
                ScheduleDays => [],
                this => $this,
                that => $that
        }
        bless $self, $class;
}
# methods go here

package ScheduleDay;

sub new {
        my $class = shift;
        my $self = {
                Trains => [],
                something => 'or other',
        }
        bless $self, $class;
}
# methods go here

package Train;
sub new {
        my $class = shift;
        my ($name, $teacher) = @_;
        my $self = {
                Members => [],
                Name => $name,
                Teacher => $teacher,
        }
        bless $self, $class;
}
# methods go here

How un-self-documenting is this?

If you want to print the list of members of a Train you use this:

        print join(', ', @{$train->{Members}),"\n";

Let's print all trains of a day:

        foreach my $train (@{$scheduleday->{Trains}}) {
                print "$train->{Name} with $train->{Teacher}\n";
                print "\t", join("\n\t", @{$train->{Members}), "\n\n";
        }

I admit the bless() looks a bit silly, the Perl calling convention 
may confuse new people, but basicaly this all is very simple.

The one thing you just have to get out of your mind is the fixed 
structs. There is (unles you use a module that will restrict you) 
nothing like that in Perl. You can add as many fields as you like at 
any time. 

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to