[BEGINNER]Storable::dclone question

S

soup_or_power

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

Jim Gibson

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.
 
S

soup_or_power

Jim said:

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

Jim Gibson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top