JSON::JOM::Plugins - plugin architecture
Most of the interesting JOM functionality is designed to be implemented via plugins.
Here's a skeleton plugin:
package JSON::JOM::Plugins::ListUtils; sub extensions { my ($class) = @_; return ( ## [ target, method, coderef ] , ['ARRAY', 'count', sub { return scalar @{$_[0]}; }], ['ARRAY', 'values', sub { return @{$_[0]}; }], ['HASH', 'count', sub { return scalar keys %{$_[0]}; }], ['HASH', 'keys', sub { return keys %{$_[0]}; }], ['HASH', 'values', sub { return values %{$_[0]}; }], ); } 1;
This adds methods count and values to JSON::JOM::Array objects and count, keys and values to JSON::JOM::Object objects.
count
values
keys
Valid targets for plugins are 'ARRAY', 'HASH', 'NULL', 'BOOLEAN', 'NUMBER' and 'STRING'. The target 'NODE' is effectively a wild card. Differentiating between numbers and strings may not always work as expected, so it's recommended that any plugin that targets numbers should also target strings, and vice versa.
e.g.
use JSON::JOM 'to_jom'; my $jom = to_jom([1,2,3,4,5,6,7,8,9]); printf("Object has %d values:\n", $jom->count); print "$_\n" foreach $jom->values;
JOM plugins must be named JSON::JOM::Plugins::pluginname or else JOM won't be able to load them.
Note that if you're developing code that uses JOM plugins, you don't need to do anything to load a plugin - JOM automatically loads all installed plugins. However, you can require or use a plugin if you want to double-check that it's installed before continuing:
require
use
my $jom = to_jom({...}); require JSON::JOM::Plugins::Dumper; print $jom->dump;
Please report any bugs to http://rt.cpan.org/.
JSON::JOM.
Toby Inkster <tobyink@cpan.org>.
Copyright 2010 Toby Inkster
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install JSON::JOM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::JOM
CPAN shell
perl -MCPAN -e shell install JSON::JOM
For more information on module installation, please visit the detailed CPAN module installation guide.