[BEGINNER} Question

S

soup_or_power

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

Mark Clements

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
 
S

soup_or_power

Mark said:
It helps if you choose a more descriptive subject line.

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

Sherm Pendley

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

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top