For such simple class (no inheritance ...), you can just
use the standard module Class::Struct, 
it will built constructor and accessors for you.
Then like Paul said :
"Just write a method (called 'init' in the example below)
which accepts the string,splits on the semi-colon,
and assigns the result to the appropriate fields".

Example :

= Person.pm file

package Person;
use strict;
use Class::Struct;
Class::Struct::struct(
        cname  => '$',
        sname  => '$',
        street => '$',
        city     => '$',
        phone  => '$',
        email  => '$'
        );

sub init{
        my($self,$str)=@_;

        #i assume here that $str is in the correct format (; separated)
        my($cname,$sname,$street,$city,$phone,$email)=split /\;/,$str
        $self->cname($cname);
        $self->sname($sname);
        $self->street($street);
        $self->city($city);
        $self->phone($phone);
        $self->email($email);
}

= person.pl file

#!/usr/bin/perl -w

use strict;
use Person;

$operson=Person->new();

open(FH,$filename) || die "blabla : $!\n";
while(<FH>){
        chomp;
        $operson->init($_);
        #do what you want with data ...
        #example: $operson->manipulate_data();
}
Close(FH);


José.

> -----Original Message-----
> From: Nicole Seitz [mailto:[EMAIL PROTECTED]] 
> Sent: Wednesday, November 27, 2002 1:13 AM
> To: [EMAIL PROTECTED]
> Subject: OOP
> 
> 
> Hi there!
> 
> I've just started to learn OOP with Perl.With the help of 
> perltoot I wrote a 
> class Person and some methods(see below).
> 
> package Person;
> use strict;
> 
> sub new {
>     my $self = {};
>     my $proto = shift;
>     my $class = ref($proto) || $proto;
>     $self->{CNAME}           = undef;
>     $self->{SNAME}           = undef;
>     $self->{STREET}          = undef;
>     $self->{CITY}            = undef;
>     $self->{PHONE}           = undef;
>     $self->{EMAIL}           = undef;
> 
>     bless ($self, $class);
>     return $self;
> 
> }
> 
> sub cname {
>     my $self = shift;
>     if (@_) {$self->{CNAME} = shift }
>     return $self->{CNAME};
> }
> 
> sub sname {
>     my $self = shift;
>     if (@_) {$self->{SNAME} = shift }
>     return $self->{SNAME};
> }
> 
> sub street {
>     my $self = shift;
>     if (@_) {$self->{STREET} = shift }
>     return $self->{STREET};
> }
> 
> sub city {
>     my $self = shift;
>     if (@_) {$self->{CITY} = shift }
>     return $self->{CITY};
> }
> sub phone {
>     my $self = shift;
>     if (@_) {$self->{PHONE} = shift }
>     return $self->{PHONE};
> }
> 
> To store some data in my object I did the follwing:
> 
> use Person;
> 
> $myPerson = Person->new();
> $myPerson->cname("John");
> $myPerson->sname("Smith");
> $myPerson->street("Euston Road");
> $myPerson->city("London");
> $myPerson->phone("414 3344");
> 
> 
> Do I really need all these methods?
> I get my data from an comma separated file;lines in this file 
> look like this: ;John;Smith;Euston Road;London;414 3344;;
> 
> So, after opening the file and reading from it line per line, 
> what's the best way to store the data in an object? Can I do 
> it in one step?I 
> mean, without using several methods(cname,sname,...)???
> 
> Many thanx in advance!
> 
> Nicole
> 
> 
> 
> 
> 
> -- 
> Was immer du tun kannst oder erträumst zu können, beginne es. 
> Kühnheit besitzt Genie, Macht und magische Kraft. Beginne es jetzt. 
> (Johann Wolfgang Goethe)
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


**** DISCLAIMER ****

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


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

Reply via email to