Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

What is the use de-referencing and creating a new anonymous hash in new() and rnew() ?

by sathiya.sw (Monk)
on Nov 28, 2008 at 14:40 UTC ( [id://726597]=perlquestion: print w/replies, xml ) Need Help??

sathiya.sw has asked for the wisdom of the Perl Monks concerning the following question:

I have been going through the module Devel::Symdump.
There i have seen a line of code which i would want a better clarification.,
my $self = bless {%${"$class\::Defaults"}}, $class;
It is a reference and dereferencing operation ? Am i right?? Or else some thing is also achieved here?
Why this is needed ? What is the intension in doing this ??
Sathiyamoorthy

Replies are listed 'Best First'.
Re: What is the use de-referencing and creating a new anonymous hash in new() and rnew() ?
by almut (Canon) on Nov 28, 2008 at 15:10 UTC

    ${"$class\::Defaults"} is a symbolic reference to the Defaults hash in the respective class (Devel::Symdump here).  The % dereferences it, so the resulting hash's flattened content will initialise the anonymous hash {...} which is going to hold the instance data of the object that is created by blessing the hashref into $class.

Re: What is the use de-referencing and creating a new anonymous hash in new() and rnew() ?
by ysth (Canon) on Nov 28, 2008 at 16:52 UTC
    This is a poor way of going about things. As noted, a subclass can have its own Defaults. The problem is that a subclass does not default to having the same defaults as its superclass. This would be better:
    { my %Defaults = ...; sub Defaults { \%Defaults } } sub new { my $class = shift; my $self = bless { %{ $class->Defaults } }, $class;
Re: What is the use de-referencing and creating a new anonymous hash in new() and rnew() ?
by zwon (Abbot) on Nov 28, 2008 at 14:58 UTC
    Symdump contains Defaults hash with default values for object. This code creates a copy of this hash and blesses it into class. Note that subclass can define its own Defaults and this still will work.
Re: What is the use de-referencing and creating a new anonymous hash in new() and rnew() ?
by pobocks (Chaplain) on Nov 29, 2008 at 10:15 UTC

    Maybe I was the only one thick enough to have to take a minute to figure this out, but the '\' there is to prevent interpretation of '::' as the package directory separator, and enforce interpolation of '$class' instead of '$class::Defaults'.

    Again, you probably all knew that, or realized it from other comments... but it threw me for a 2-second loop.

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://726597]
Approved by marto
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found