<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>At the recent OSDC I heard about Moose so I thought I'd take the Moose out on the hunting range to see what I could come up with. Now, I also wanted to fiddle a bit with JSON so I thought, well, let's do some TDD as practice and see what happens. Here's a cut down version of TestJSON.pm:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"># This is the actual module MINUS the POD - the POD is all there in the<br># real version, I just feel it's unecessary to post.<br>package TestJSON;<br><br>our $VERSION = '0.01';<br><br>use Moose;<br><br>use Carp;<br>use Data::Dumper;<br><br>use JSON;<br><br>has "debug" => (<br> is => "rw",<br> isa => "Int",<br> default => 1,<br>);<br><br>sub json2perl {<br> my ($self, $perl) = @_;<br> <br> return from_json($perl);<br>}<br><br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">sub perl2json {</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><span class="Apple-tab-span" style="white-space:pre">        </span>my ($self, $hash) = @_;</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><span class="Apple-tab-span" style="white-space:pre">        </span>return (if !defined $hash || ref($hash) ne "HASH");</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><span class="Apple-tab-span" style="white-space:pre">        </span>return to_json($hash);</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">}</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br>1;</blockquote><br><div>Now, the JSON module export from_json and to_json by default.</div><div><br></div><div>The curious thing that I've noticed is that this works:</div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"> DB<5> david-lloyds-imac:TestJSON lloy0076$ perl -Ilib -MTestJSON -d -e 0</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote>Loading DB routines from perl5db.pl version 1.28<br>Editor support available.<br><br>Enter h or `h h' for help, or `man perldebug' for more help.<br><br>main::(-e:1):<span class="Apple-tab-span" style="white-space:pre">        </span>0<br>DB<1> $tj = TestJSON->new();<br><br>DB<2> $tj->from_json('{"event":"suiteStart","suite":"ArrayTest","tests":2}');<br>malformed JSON string, neither array, object, number, string or atom, at character offset 0 ["TestJSON=HASH(0xbbd9..."] at /Library/Perl/5.8.8/JSON.pm line 154.</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">^^^ BTW, that is valid JSON, see below</blockquote><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote>It appears that "from_json" is being interpreted as an object method and then dispatched to JSON::from_json like this:<div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;">JSON::from_jason($object_hash, $json);</blockquote><br><div>Take careful note that the object hash is now in the array. The function then reads the very first argument passed to it and then balks.</div><div><br></div><div>In another session I managed this:</div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><br></blockquote>DB<3> $tj->to_json({a => 'b'});<br>Can't locate object method "a" via package "JSON" at /Library/Perl/5.8.8/JSON.pm line 136.</blockquote><br><div>So, a few questions:</div><div><div><br></div><div><ol class="MailOutline"><li>If one uses a module in Perl 5.8.10 inside an object, but more specifically a Moose based object, and it imports functions into the module's namespace, it appears that the functions really <b>are</b> in the namespace and can be called</li><ol class="MailOutline"><li>But the caveat is that they don't do what you expect them to do...</li></ol><li>Interestingly if I remove the perl2json or json2perl class, the class methods "to_json" and "from_json" respectively (or both) throw a "can't be found in namespace error", i.e. Moose/Perl can't find them</li><ol class="MailOutline"><li>Which implies they're being loaded because they're used in the module...</li><li>I've looked into JSON's source code and it seems to be doing some funky stuff to figure out which backend JSON:: one is using but other than that it looks like a standard Exporter to me, it just has rolled its own export and import functions</li></ol><li>As a matter of OO style, is it "nice" to expose "used" package's imports in the class one is making even though the behaviour if you use them as a class method appears to be strange</li><ol class="MailOutline"><li>That is, in this case I did "use JSON;" and caused "from_json" and "to_json" to materialise - if I have an actual object (TestJSON->new()) I can call them but as shown the behaviour is suboptimal</li></ol></ol><div><br></div><div>I'm just wondering.</div><div><br></div><div>DSL</div></div></div></body></html>