Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > [BEGINNER]Storable::dclone question

Reply
Thread Tools

[BEGINNER]Storable::dclone question

 
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      03-25-2005
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;
}

 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      03-26-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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 =---
 
Reply With Quote
 
 
 
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      03-28-2005

Jim Gibson wrote:
> In article <(E-Mail Removed) .com>,
> <(E-Mail Removed)> 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 =---


 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      03-29-2005
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> wrote:

> Jim Gibson wrote:
> > In article <(E-Mail Removed) .com>,
> > <(E-Mail Removed)> 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 =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments