Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[BEGINNER]Storable::dclone question

100 views
Skip to first unread message

soup_o...@yahoo.com

unread,
Mar 25, 2005, 5:46:35 PM3/25/05
to
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;
}

Jim Gibson

unread,
Mar 25, 2005, 9:13:25 PM3/25/05
to
In article <1111790795.5...@l41g2000cwc.googlegroups.com>,
<soup_o...@yahoo.com> 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 =---

soup_o...@yahoo.com

unread,
Mar 28, 2005, 6:23:35 PM3/28/05
to

Jim Gibson wrote:
> In article <1111790795.5...@l41g2000cwc.googlegroups.com>,
> <soup_o...@yahoo.com> 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 );

0 new messages