[BEGINNER]Storable::dclone question

Discussion in 'Perl' started by soup_or_power@yahoo.com, Mar 25, 2005.

  1. Guest

    Can someone explain this code especially what it is doing with
    Storable::dclone? Thanks in advance

    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;
    }
    , Mar 25, 2005
    #1
    1. Advertising

  2. Jim Gibson Guest

    In article <>,
    <> wrote:

    > Can someone explain this code especially what it is doing with
    > Storable::dclone? Thanks in advance
    >
    > 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 ) : { };


    If $self is a reference (to an object instance in this case), then
    dclone($self) makes a deep-clone copy of that object and assigns it (a
    new reference) to $obj, which then is blessed as a new object.

    If $self is not a reference, it is a package, and $obj becomes a new,
    empty object (reference to empty hash).

    > 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;
    > }
    >


    FYI: this newsgroup is defunct; please do not start threads here. Try
    comp.lang.perl.misc in the future.


    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    Jim Gibson, Mar 26, 2005
    #2
    1. Advertising

  3. Guest

    Jim Gibson wrote:
    > In article <>,
    > <> wrote:


    Thank you! Since the thread is started here I will ask another related
    question (before I switch to comp.lang.perl.misc :). Kindly clarify
    what freeze and thaw are doing below. Many thanks!!!!

    # -------------------------------------------------------
    # Class Constructor
    # -------------------------------------------------------

    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;
    }

    # -------------------------------------------------------
    # Deep (Recursive) Clone Object Instance
    # -------------------------------------------------------

    sub dclone {
    my ($self) = @_;
    return &Storable::dclone($self);
    }

    # -------------------------------------------------------
    # Serialize Object Instance
    # -------------------------------------------------------

    sub freeze {
    my ($self) = @_;
    return &Storable::nfreeze($self);
    }

    # -------------------------------------------------------
    # Inflate Object Instance (Class/Object Method)
    # -------------------------------------------------------

    sub thaw {
    my ($self, $buffer) = @_;
    return &Storable::thaw( $buffer );
    }

    > > Can someone explain this code especially what it is doing with
    > > Storable::dclone? Thanks in advance
    > >
    > > 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 ) : { };

    >
    > If $self is a reference (to an object instance in this case), then
    > dclone($self) makes a deep-clone copy of that object and assigns it

    (a
    > new reference) to $obj, which then is blessed as a new object.
    >
    > If $self is not a reference, it is a package, and $obj becomes a new,
    > empty object (reference to empty hash).
    >
    > > 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;
    > > }
    > >

    >
    > FYI: this newsgroup is defunct; please do not start threads here. Try
    > comp.lang.perl.misc in the future.
    >
    >
    > ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet

    News==----
    > http://www.newsfeeds.com The #1 Newsgroup Service in the World!
    >100,000 Newsgroups
    > ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
    , Mar 29, 2005
    #3
  4. Jim Gibson Guest

    In article <>,
    <> wrote:

    > Jim Gibson wrote:
    > > In article <>,
    > > <> wrote:

    >
    > Thank you! Since the thread is started here I will ask another related
    > question (before I switch to comp.lang.perl.misc :). Kindly clarify
    > what freeze and thaw are doing below. Many thanks!!!!


    The methods dclone, freeze, and thaw are all simple wrappers to the
    similarly-named (but not exact in the case of freeze) methods of
    Storable. For more information on what they do, you should consult the
    documentation for the Storable module.

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Jim Gibson, Mar 29, 2005
    #4
    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. Daniel Hembree
    Replies:
    0
    Views:
    1,182
    Daniel Hembree
    Jan 12, 2004
  2. TonyShirt

    dclone and copying one element of a hash

    TonyShirt, May 4, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    93
    TonyShirt
    May 7, 2004
  3. A. Sinan Unur

    dclone alternatives

    A. Sinan Unur, Feb 23, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    93
    A. Sinan Unur
    Feb 24, 2006
  4. Graham Wood

    where to put dclone

    Graham Wood, Sep 19, 2006, in forum: Perl Misc
    Replies:
    8
    Views:
    116
    Graham Wood
    Sep 22, 2006
  5. arun
    Replies:
    1
    Views:
    1,376
    Rainer Weikusat
    Mar 8, 2012
Loading...

Share This Page