Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   [BEGINNER} Question (http://www.velocityreviews.com/forums/t891652-beginner-question.html)

soup_or_power@yahoo.com 04-04-2005 02:01 PM

[BEGINNER} Question
 
Can someone explain what's this code doing at a meta level? Thanks

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


Mark Clements 04-04-2005 02:23 PM

Re: [BEGINNER} Question
 
soup_or_power@yahoo.com wrote:
> Can someone explain what's this code doing at a meta level? Thanks

It helps if you choose a more descriptive subject line.

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

If by "meta level" you mean "what is the subroutine actually supposed to
do", it is defining a constructor method (though I have issues with the
way it is done). Check out

man perltoot
man perlobj

It then populates the attributes of the newly created object with
attribute names and values passed in in $hash, though it doesn't do this
for some reason (Perl itself doesn't care) if the value itself is a
reference.

Is this homework?

Mark

Tad McClellan 04-04-2005 03:37 PM

Re: [BEGINNER} Question
 
soup_or_power@yahoo.com <soup_or_power@yahoo.com> wrote:

> Subject: [BEGINNER} Question



Please put the subject of your article in the Subject of your article.

Your post is not about beginner questions.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

soup_or_power@yahoo.com 04-05-2005 12:47 AM

Re: [BEGINNER} Question
 

Mark Clements wrote:
> soup_or_power@yahoo.com wrote:
> > Can someone explain what's this code doing at a meta level? Thanks

> It helps if you choose a more descriptive subject line.
>
> > 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;
> > }
> >

> If by "meta level" you mean "what is the subroutine actually supposed

to
> do", it is defining a constructor method (though I have issues with

the
> way it is done). Check out
>
> man perltoot
> man perlobj
>
> It then populates the attributes of the newly created object with
> attribute names and values passed in in $hash, though it doesn't do

this
> for some reason (Perl itself doesn't care) if the value itself is a
> reference.
>
> Is this homework?


No this is not homework. I'm hired to do a perl project. There is lots
of code already developed. I've 2 years of Perl4.0 and am finding it
difficult to digest the posted code. I wonder why the object is being
cloned. In Java the objects are serialized to disk/network. Is this the
same kind of thing going on here? I'm lost. Thank you for your help.


Sherm Pendley 04-05-2005 02:54 AM

Re: [BEGINNER} Question
 
soup_or_power@yahoo.com wrote:

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


> difficult to digest the posted code. I wonder why the object is being
> cloned.


Essentially, it's a dual-purpose constructor. If it's called as a class
method, it creates a new object. If it's called as instance method, it
creates a copy of itself.

You'll find a *lot* of people who don't care much for this technique. As
you've found out, it can be confusing. It's clearer to write two methods
- a class method new() for creating new objects, and an instance method
copy() for making copies.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


All times are GMT. The time now is 01:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.