[Linux-aus] Moose, JSON Question

David Lloyd lloy0076 at adam.com.au
Sat Jan 3 19:01:57 EST 2009


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:

# This is the actual module MINUS the POD - the POD is all there in the
# real version, I just feel it's unecessary to post.
package TestJSON;

our $VERSION = '0.01';

use Moose;

use Carp;
use Data::Dumper;

use JSON;

has "debug" => (
     is => "rw",
     isa => "Int",
     default => 1,
);

sub json2perl {
     my ($self, $perl) = @_;

     return from_json($perl);
}

sub perl2json {
	my ($self, $hash) = @_;

	return (if !defined $hash || ref($hash) ne "HASH");

	return to_json($hash);
}

1;

Now, the JSON module export from_json and to_json by default.

The curious thing that I've noticed is that this works:

   DB<5> david-lloyds-imac:TestJSON lloy0076$ perl -Ilib -MTestJSON -d  
-e 0

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):	0
DB<1> $tj = TestJSON->new();

DB<2> $tj- 
 >from_json('{"event":"suiteStart","suite":"ArrayTest","tests":2}');
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.

^^^ BTW, that is valid JSON, see below

It appears that "from_json" is being interpreted as an object method  
and then dispatched to JSON::from_json like this:

JSON::from_jason($object_hash, $json);

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.

In another session I managed this:

DB<3> $tj->to_json({a => 'b'});
Can't locate object method "a" via package "JSON" at /Library/Perl/ 
5.8.8/JSON.pm line 136.

So, a few questions:

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 are in the  
namespace and can be called
But the caveat is that they don't do what you expect them to do...
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
Which implies they're being loaded because they're used in the module...
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
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
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

I'm just wondering.

DSL
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linux-aus/attachments/20090103/7ea610ec/attachment.htm 


More information about the linux-aus mailing list