A weird thing just bit be. I think this is what happened.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.