[BEGINNER} Question

Discussion in 'Perl Misc' started by soup_or_power@yahoo.com, Apr 4, 2005.

  1. Guest

    Can someone explain what's this code doing at a meta level? Thanks

    sub new {
    my ($self, $hash) = @_;

    # ------------------------------------------------------
    # Create new hash ref/obj ref (Copy Constructor)
    # ------------------------------------------------------

    my $type = ref($self) || $self;
    my $obj = ( ref $self ) ? &Storable::dclone( $self ) : { };
    my $new = bless $obj, $type;

    # ------------------------------------------------------
    # Initialize properties (copy $hash into $new)
    # ------------------------------------------------------

    if ($hash && ref $hash) {
    while ( my ($key, $value) = each %$hash ) {
    $new->{$key} = $value if ! ref $value;
    }
    }

    return $new;
    }
     
    , Apr 4, 2005
    #1
    1. Advertising

  2. wrote:
    > Can someone explain what's this code doing at a meta level? Thanks

    It helps if you choose a more descriptive subject line.

    > sub new {
    > my ($self, $hash) = @_;
    >
    > # ------------------------------------------------------
    > # Create new hash ref/obj ref (Copy Constructor)
    > # ------------------------------------------------------
    >
    > my $type = ref($self) || $self;
    > my $obj = ( ref $self ) ? &Storable::dclone( $self ) : { };
    > my $new = bless $obj, $type;
    >
    > # ------------------------------------------------------
    > # Initialize properties (copy $hash into $new)
    > # ------------------------------------------------------
    >
    > if ($hash && ref $hash) {
    > while ( my ($key, $value) = each %$hash ) {
    > $new->{$key} = $value if ! ref $value;
    > }
    > }
    >
    > return $new;
    > }
    >

    If by "meta level" you mean "what is the subroutine actually supposed to
    do", it is defining a constructor method (though I have issues with the
    way it is done). Check out

    man perltoot
    man perlobj

    It then populates the attributes of the newly created object with
    attribute names and values passed in in $hash, though it doesn't do this
    for some reason (Perl itself doesn't care) if the value itself is a
    reference.

    Is this homework?

    Mark
     
    Mark Clements, Apr 4, 2005
    #2
    1. Advertising

  3. <> wrote:

    > Subject: [BEGINNER} Question



    Please put the subject of your article in the Subject of your article.

    Your post is not about beginner questions.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 4, 2005
    #3
  4. Guest

    Mark Clements wrote:
    > wrote:
    > > Can someone explain what's this code doing at a meta level? Thanks

    > It helps if you choose a more descriptive subject line.
    >
    > > sub new {
    > > my ($self, $hash) = @_;
    > >
    > > # ------------------------------------------------------
    > > # Create new hash ref/obj ref (Copy Constructor)
    > > # ------------------------------------------------------
    > >
    > > my $type = ref($self) || $self;
    > > my $obj = ( ref $self ) ? &Storable::dclone( $self ) : { };
    > > my $new = bless $obj, $type;
    > >
    > > # ------------------------------------------------------
    > > # Initialize properties (copy $hash into $new)
    > > # ------------------------------------------------------
    > >
    > > if ($hash && ref $hash) {
    > > while ( my ($key, $value) = each %$hash ) {
    > > $new->{$key} = $value if ! ref $value;
    > > }
    > > }
    > >
    > > return $new;
    > > }
    > >

    > If by "meta level" you mean "what is the subroutine actually supposed

    to
    > do", it is defining a constructor method (though I have issues with

    the
    > way it is done). Check out
    >
    > man perltoot
    > man perlobj
    >
    > It then populates the attributes of the newly created object with
    > attribute names and values passed in in $hash, though it doesn't do

    this
    > for some reason (Perl itself doesn't care) if the value itself is a
    > reference.
    >
    > Is this homework?


    No this is not homework. I'm hired to do a perl project. There is lots
    of code already developed. I've 2 years of Perl4.0 and am finding it
    difficult to digest the posted code. I wonder why the object is being
    cloned. In Java the objects are serialized to disk/network. Is this the
    same kind of thing going on here? I'm lost. Thank you for your help.
     
    , Apr 5, 2005
    #4
  5. wrote:

    >>>sub new {
    >>> my ($self, $hash) = @_;
    >>>
    >>> # ------------------------------------------------------
    >>> # Create new hash ref/obj ref (Copy Constructor)
    >>> # ------------------------------------------------------
    >>>
    >>> my $type = ref($self) || $self;
    >>> my $obj = ( ref $self ) ? &Storable::dclone( $self ) : { };
    >>> my $new = bless $obj, $type;
    >>>
    >>> # ------------------------------------------------------
    >>> # Initialize properties (copy $hash into $new)
    >>> # ------------------------------------------------------
    >>>
    >>> if ($hash && ref $hash) {
    >>> while ( my ($key, $value) = each %$hash ) {
    >>> $new->{$key} = $value if ! ref $value;
    >>> }
    >>> }
    >>>
    >>> return $new;
    >>>}


    > difficult to digest the posted code. I wonder why the object is being
    > cloned.


    Essentially, it's a dual-purpose constructor. If it's called as a class
    method, it creates a new object. If it's called as instance method, it
    creates a copy of itself.

    You'll find a *lot* of people who don't care much for this technique. As
    you've found out, it can be confusing. It's clearer to write two methods
    - a class method new() for creating new objects, and an instance method
    copy() for making copies.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Apr 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerker Hammarberg

    Beginner question: What trigs processes

    Jerker Hammarberg, Jul 17, 2003, in forum: VHDL
    Replies:
    16
    Views:
    1,629
    Mike Treseler
    Jul 22, 2003
  2. smu
    Replies:
    3
    Views:
    2,640
    shobhit24
    Jun 28, 2006
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    597
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    984
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    227
    7stud --
    Apr 12, 2011
Loading...

Share This Page