Rob Dixon wrote:

> ...
> Yes, but it's
>
>     $schedule_day_object->AddTrain($train_name);
>
> which does that for you.
>
> ...

> It's a tradition that methods start with:
>
>     my $self = shift;
>     my ( $trainName, $p1, $p2, $p3, @prest ) = @_;
>
> to separate the implicit parameter from the explcit one.
> But what Rob R wrote will do just as well.

Hi Rob,

Well, you goaded me into it.  I finally decided to pull the spoon off my tongue and 
try Perl OOP.  I must say, it is pretty painless.  I can get used to the C-style 
arrows, though I'd prefer to use a dot operator as in C++, and the bless() function is 
only mildly fetishistic.  All in all, object creation is pretty straightforward, 
though.

But...

I can do this:

#!/usr/bin/perl -w

use strict;

use Person;

my $person = Person::new("Newton", "R. Joseph");
$person->set_address("666 NoSpam way", "Eugene", "OR", "97402",
 "USA");
$person->{'Surname'} = "Johnson";     #this
$person->print_name();
$person->{'What the heck'} = "Jeez, this is a porous guy!";  # and this
print "$person->{'What the heck'}\n";

Without anything preventing me.  I am not at all sure that this is a good thing.  Is 
there any way to construct a class so that member data stays private?  This was the 
factor that kept me from venturing into Perl OOP, and it still seems like it could 
lead to problems if the only thing preventing this kind of external access is the 
injuction "Don't do that!".

Joseph
package Person;

use strict;
use warnings;
use Address;

sub new {
  my $self = {
    'Surname' => $_[0],
    'Given Name' => $_[1]
  };
  bless $self;
}

sub print_name {
  my $self = shift;
  print "$self->{'Surname'}, ";
  print "$self->{'Given Name'}\n";
  return unless defined $self->{'Address'};
#  print "$self->{'Address'}->{'Street'}\n";
#  print "$self->{'Address'}->{'City'}, ";
#  print "$self->{'Address'}->{'State'}  ";
#  print "$self->{'Address'}->{'PostCode'}\n";
#  print "$self->{'Address'}->{'Country'}\n" if
#  defined $self->{'Address'}->{'Country'};
  $self->{'Address'}->print();
}

sub set_address {
  my $self = shift;
  my $address = Address::new(@_);
#  $address{'Street'} = $_[0];
#  $address{'City'} = $_[1] if defined $_[1];
#  $address{'State'} = $_[2] if defined $_[2];
#  $address{'PostCode'} = $_[3] if defined $_[3];
#  $address{'Country'} = $_[4] if defined $_[4];
  $self->{'Address'} = $address;
}

#my $conclusion = "Person has a name";
1;
__END__  
package Address;

use strict;
use warnings;

sub new {
  my $self = {
    'Street' => $_[0],
  };
  $self->{'City'} = $_[1] if defined $_[1];
  $self->{'State'} = $_[2] if defined $_[2];
  $self->{'PostCode'} = $_[3] if defined $_[3];
  $self->{'Country'} = $_[4] if defined $_[4];
  bless $self;
}

sub print {
  my $self = shift;
  print "$self->{'Street'}\n";
  print "$self->{'City'}, ";
  print "$self->{'State'}  ";
  print "$self->{'PostCode'}\n";
  print "$self->{'Country'}\n" if
  defined $self->{'Country'};
}

my $exultation = 'Whee-haw!!  we got adrresses, podner!';
__END__

Attachment: PersonTest.pl
Description: Perl program

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

Reply via email to