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