prjain has asked for the wisdom of the Perl Monks concerning the following question:

#file name person.pm package person; sub new { $class = shift; $self = { Name => "", Email_id =>"", Contact => "", Company =>"", #spouce =>, # Spouce class pointer (default 'no spouce') }; bless ($self,$class); return $self; }; sub Name { $self = shift; if(@_) { $self->{Name} = shift;} return $self->{Name}; }; sub Email_id { $self = shift; if(@_) { $self->{Email_id} = shift ;} return $self->{Email_id} }; : : : sub spouce { $self=shift; if(@_) { $self->{spouce} = shift ;} return $self->{spouce} }; sub PrintDetail { $self=shift; print STDOUT "Self\n Name = $self->{Name}\n Email=$self->{Email_ +id}\n Contact=$self->{Contact},\n Company=$self->{Company}\n"; if ($self->{spouce}) { print STDOUT "Spouce\n Name = $self->{spouce}->{Name}\n Emai +l=$self->{spouce}->{Email_id}\n Contact=$self->{spouce}->{Contact},\ +n Company=$self->{spouce}->{Company}\n"; }; } #person_test.pl use person; open(INFILE,$file)|| die "could not open file for reading!\n"; while(<INFILE>) { chomp $_; ($Name,$eMail,$Mobile,$Company,$Spouse,$SeMail,$SMobile,$SComp +any,$NativePlace) = split(/,/); #print "$i) $Name :\t $Mobile :\t $eMail\n"; foreach($Name) { if($_ =~ m/^[$pattern]/i) { $object = Person->new(); $object->Name($Name); $object->Email_id($eMail); $object->Contact($Mobile); $object->Company($Company);}
In this code I hv to access data frm a comma separated file dat contain fields as Name , Emailid,contact,comapany, Spouse ,SEmail_id , S_contact,S_comapny etc here I have a person class that hs a hash with fields as Name, Email, contact, Spouse. Now I can access data in person_test.pl file frm csvfile and save the person information in hash eaisily. Bt this code will print the spouse information along with person information only when dere is some spouse exists. And to print the spouse information I need to use already existing variables as Spouse is also a person,I need to implement this widout using inheritance.I m nt sure how to implement this plz give me some idea. Thanks in advance ..

Replies are listed 'Best First'.
Re: how to use hash that points to the same hash?
by CountZero (Bishop) on Mar 07, 2012 at 19:28 UTC
    OK. I take the challenge. Helping ladies (especially pretty ladies) in distress is my foible. ;)

    I really do not see what --conceptually-- is your problem.

    One of the fields in your record structure holds the name of the spouse. Assuming the name is a key field in your record structure, the "spouse" field is a link to the record which holds the personal data of the spouse. No problem at all to implement that in a database or in a hash (or rather a hash of hashrefs).

    However, your implementation seems ... strange. You actually make each record into an object of the Person-class. So far so good.

    But of course, such an object can only hold one instance at the same time and you are overwriting the same object each time with new data.

    I am not sure that using objects is the right way forward here. A possible solution could be to save each new object into a hash (the key would be the name and the value the object), but to me that seems a huge waste of effort for a small return. Why not just saving the data in a database and if you really want to go the OOP-way, access the database with module::DBIx::Class.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: how to use hash that points to the same hash?
by shridhar22 (Novice) on Mar 07, 2012 at 18:58 UTC
    #file name person.pm package person; sub new { $class = shift; $self = { Name => "", Email_id =>"", Contact => "", Company =>"", #spouce =>, # Spouce class pointer (default 'no spouce') }; bless ($self,$class); return $self; }; sub Name { $self = shift; if(@_) { $self->{Name} = shift;} return $self->{Name}; }; sub Email_id { $self = shift; if(@_) { $self->{Email_id} = shift ;} return $self->{Email_id} }; : : : sub spouce { $self=shift; if(@_) { $self->{spouce} = shift ;} return $self->{spouce} }; sub PrintDetail { $self=shift; print STDOUT "Self\n Name = $self->{Name}\n Email=$self->{Email_ +id}\n Contact=$self->{Contact},\n Company=$self->{Company}\n"; if ($self->{spouce}) { print STDOUT "Spouce\n Name = $self->{spouce}->{Name}\n Emai +l=$self->{spouce}->{Email_id}\n Contact=$self->{spouce}->{Contact},\ +n Company=$self->{spouce}->{Company}\n"; }; } #person_test.pl use person; open(INFILE,$file)|| die "could not open file for reading!\n"; while(<INFILE>) { chomp $_; ($Name,$eMail,$Mobile,$Company,$Spouse,$SeMail,$SMobile,$SComp +any,$NativePlace) = split(/,/); #print "$i) $Name :\t $Mobile :\t $eMail\n"; foreach($Name) { if($_ =~ m/^[$pattern]/i) { $object = Person->new(); $object->Name($Name); $object->Email_id($eMail); $object->Contact($Mobile); $object->Company($Company);}

    In this code I hv to access data frm a comma separated file dat contain fields as Name , Emailid,contact,comapany, Spouse ,SEmail_id , S_contact,S_comapny etc here I have a person class that hs a hash with fields as Name, Email, contact, Spouse. Now I can access data in person_test.pl file frm csvfile and save the person information in hash eaisily. Bt this code will print the spouse information along with person information only when dere is some spouse exists. And to print the spouse information I need to use already existing variables as Spouse is also a person,I need to implement this widout using inheritance.I m nt sure how to implement this plz give me some idea. Thanks in advance ..

      I've tried to make the question more readable for the viewers.. but dont have an idea on how to solve the issue. Guyzzz plz help this pretty lady...!!! Cheers

        It would help the "pretty lady" more if you were to suggest to her to reformat her node. If you are close by you could even use it as an excuse to show her how to do it.

        True laziness is hard work