#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 ..
In reply to Re: how to use hash that points to the same hash?
by shridhar22
in thread how to use hash that points to the same hash?
by prjain
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |