Hi, I have written small perl module and one script. In the constructor of the package, I am using one initializer method. The problem, I'm facing is: When, I create two different objects on the package, latest object's initialize methond is overwritting variables of previously created object. I dont have much knowledge on Perl. Unable to determine the cause. Requesting for some help. I am providing the code( of package and script and program output) below. thank you.

#######################################3 package MyPackage; use Data::Dumper; sub new { my $ip_addr = shift(); my $self = { "DEVICE_IP" =>$ip_addr }; bless $self, 'MyPackage'; $self->my_init(); return $self; } my $self_ip = undef; my $partner_ip = undef; my $WS_1 = undef; my $WS_2 = undef; #---------------------- sub my_init{ my $this = shift; ($WS_1, $WS_2 ) = ('10.199.35.11' , '10.199.35.12'); if($this->{DEVICE_IP} eq $WS_1) { $partner_ip = $WS_2; } else{ $partner_ip = $WS_1; } $self_ip = $this->{DEVICE_IP}; return $this; } #----------------- sub my_method{ my $this = shift; print "\n Local IP = $self_ip , Partner IP = $partner_ip \n"; return 1; } 1; __END__ ###################################3 #My script is as below: use MyPackage; my $local_ip= '10.199.35.11'; my $remote_ip = '10.199.35.12' ; my $objLocal = MyPackage::new($local_ip); my $ret = $objLocal->my_method(); my $objRemote = MyPackage::new($remote_ip); my $ret = $objLocal->my_method(); #####################################
# output observed is: Local IP = 10.199.35.11 , Partner IP = 10.199.35.12 Local IP = 10.199.35.12 , Partner IP = 10.199.35.11 Because, I am calling the same old object again, I am expecting the ou +tput to be: Local IP = 10.199.35.11 , Partner IP = 10.199.35.12 Local IP = 10.199.35.11 , Partner IP = 10.199.35.12

In reply to initializer method called from object constructor, overwriting the another object's variables by sree.nivas

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.