#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 how to use hash that points to the same hash? by prjain

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.