I use CGI::Session to persist an object, so I simply assign $self to a param in the CGI::Session, like this:
$session->param(self => $self);
And in the retrieve method, this happens:
if(my $newSelf = $session->param("self")) { %$self = %$newSelf; }
This worked just fine up until the moment I created a property which wasn't just a scalar value, but an object. Perl couldn't find methods on the restored object, making this fail:
my $ob = $self->myobject; $ob->mymethod(42);
with a:
Can't locate object method "mymethod" via package "My::Class" (perhaps you forgot to load "My::Class"?)
This is where I scratched my head and started to try to find what I did wrong. But nothing looked out of the ordinary. print Dumper($ob) looked nice. ref($ob) was the correct package.
I even created a new object and dumped it next to my broken one. And while they looked the same, I could call methods on the new object, so there was no problem with finding the module itself.
Even though the object looked ok it seemed to have lost something. Long shot:
bless $ob, ref($ob);
After that, calling methods worked! Whoa! What's going on here? Time to read some code!
I used the CGI::Session's Default serialization technique, which according to the docs is Data::Dumper. But what the docs don't say is that the object is not deserialized using an ordinary eval, but evaled in a Safe compartment. This seems to make the "bless" not work properly outside of the compartment, even though it seems to be of the correct class.
I'm sure there's a lesson or two to be learned here, but I'm not sure what they are (although "Don't assume", and "Open Source readability is nice" comes to mind).
/J
In reply to Safe objects by jplindstrom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |