Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOPerl bless question 2

Status
Not open for further replies.

varakal

IS-IT--Management
Mar 18, 2004
114
US
I have the following class:

Code:
package MyClass;

sub new {
        my $classname = shift;
        bless {'a'=>1,'b'=>2}, $classname;
}
1;

And I am accessing the class in my script as below:

Code:
#!/usr/bin/perl -w
use MyClass;

my $objref = MyClass->new();
print ref($objref) . "\n";
print for (keys %$objref);

The output I get is
Code:
MyClass
ab

My question is, because we can access the keys of the blessed hash (second line of output), doesnt that mean I am giving up some information to the end user? It seems like I did the bless thing right as the ref gave me the class name. Am I missing something here, or is this the way it is designed? If so, I cant store any secured information or references in the hash?

Thanks,
VaRaKal
 
we" can not access the keys to the hash, only the script can. I'm not sure what your concern is.
 
What is the general purpose that I can use keys and values in the blessed hash for? Are these used only for initialization variables that the script gives to constructor? If all private values and references are assigned to 'my' variables in the class, where does one need to use these blessed hash values?
 
Firstly, the hash is visible externally. If you read the camel book, it says "perl prefers that you don't come in because you were not invited, not because it has a shotgun behind the door".
Secondly, the has is actually an anonymous hash that is created dynamically in your constructor. If you tried to use "my" variables in the constructor, they would lose scope and vanish if not returned. If defined outside the constructor scope, they would be shared by all instances of the class.
If you really really want to hide your instance data (properties) then it is possible using a closure. The problem with that is that it is slower, much more difficult to write and understand, and it plays hell with inheritance.

The moral of the story is that if you don't need to worry about unscrupulous users accessing your data then don't worry about it.
If, on the other hand, it is crucial to hide your data then maybe an interpreted language like perl is not the right choice for your application anyway.



Trojan.
 
This is all I want to know. Thanks.
 
Thanks Trojan for that explanation, I guess I didn't and maybe still don't understand the question. Another star for you. [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top