oop - How can I stop or allow a perl object to be used as a hashref? -


i have perl class based on blessed hashref ( https://github.com/kylemhall/koha/blob/master/koha/object.pm )

this community based project, many developers of varying skill.

what i've seen developers accidentally using our objects hashrefs. actual data not stored in blessed hashref, in dbic object stored in hashref ( in $self->{_result} ). when dev tries $object->{id} perl doesn't complain, returns undef, expected.

what i'd either a) make script explode error when happens b) allow use of hashref syntax setting / getting values in dbic objects stored in $self->{_result}

i tried using:

use overload '%{}' => \&get_hashref; 

but when this, get_hashref called time regular method called! makes sense in way, since object hashref. i'm sure has perl internals blessed hashrefs objects.

is i'm trying accomplish possible?

i suggest using scalar-based or array-based object instead of hash-based object. cheap (efficient) solution since causes offender run afoul of existing type checks.

for example, object produced following reference actual object. use $$self instead of $self in methods.

$ perl -e'    sub new {       $class = shift;       $self = bless(\{}, $class);       # $$self->{...} = ...;       return $self;    }    $o = __package__->new();    $id = $o->{id}; ' not hash reference @ -e line 9. 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -