Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I make a copy constructor? (inheritance?)

by RiotTown (Scribe)
on Mar 29, 2001 at 03:57 UTC ( #68006=categorized question: print w/replies, xml ) Need Help??
Contributed by RiotTown on Mar 29, 2001 at 03:57 UTC
Q&A  > object-oriented programming


Description:

I'm fairly new to this perl-OO stuff, but I am making headway except for a recent problem. I've got a package with the follow constructor and accessor methods, but I'm running into problems with creating a copy constructor.
sub new { my $self; $self->{ A } = undef; $self->{ B } = undef; bless $self, 'Stuff'; } sub A { my ( $self, $a ) = @_; $self->{ A } = $a; }
---- I've gotten the copy() method below to work:
sub copy { my ( $self ) = @_; my ( $new ); %{ $new } = %{ $self }; bless $new, 'Stuff'; return $new; }
The problem that I'm running into is that in the Stuff object I'd like $self->{ B } to be an array of another type of object (same type of structure, much different data). Whenever I call the copy constructor I just get a reference to the same array as the initial object, not a true copy. Any ideas as to what I'm doing wrong, or am I approaching this the wrong way?

Answer: How do I make a copy constructor? (inheritance?)
contributed by japhy

You want a deep copy -- try the Storable module; you want the dclone() function.

Answer: How do I make a copy constructor? (inheritance?)
contributed by BazB

merlyn (otherwise known as Randal Schwartz) discusses shallow vs deep copying in one of his columns.

An alternative to Storable for creating clones/copies of objects is the Clone module.

Answer: How do I make a copy constructor? (inheritance?)
contributed by Anonymous Monk

There is a potential other problem with your code, which I would like to pinpoint as you have talked about inheritance. If your copy method is called for a child of the Stuff class it will generate a Stuff object and not the a Child-Of-Stuff object. In order to avoid this you could bless to ref($self) you might considder it unlikely that a child class will not override the copy constructor, but reasonable applications are thinkable. (e.g.dispatching by class, Stuff is abstract/interface and you just add methods in your child classes etc.) And there is a not OO issue, what if you change the name of your class, you have to change all your bless statements... It is in general a good idea not to hardcode this kind of information.

Please (register and) log in if you wish to add an answer



  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    [Corion]: Ah - if that information is saved in a file, then you could theoretically spam that file and confuse getlogin(). So, don't use it for authentication :)
    [tye]: that is what getlogin() certainly *used* to do. I don't believe that is what it certainly should do.
    [davido]: /var/run/utmp is 664 i think.
    [tye]: Note that my "man getlogin" says that it uses stdin when it should use /dev/tty (calling a glibc bug). But that does not appear to be the case when I test it. But maybe Perl's getlogin() is not using glibc's getlogin().
    [oiskuu]: well, run a strace and see what the getlogin does for you.... As I said. SELinux probably has those security labels. But not regular linux.
    [tye]: for example, read https://unix. stackexchange.com/ questions/146138/ loginuid-should-be -allowed-to-change -or-not-mutable-or -not
    [tye]: I'm not using SELinux and it certainly appears to disagree with you. shrug
    [tye]: Since you brought up /proc, oiskuu, I didn't see you respond to my suggestion of 'loginuid'. Does your /proc not have such?

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2017-06-23 19:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many monitors do you use while coding?















      Results (554 votes). Check out past polls.