App::PipeFilter::JsonSort - a sort(1)-like filter that understands JSON fields
Here is the jsort(1) pipeline filter.
#!/usr/bin/perl use App::PipeFilter::JsonSort; exit App::PipeFilter::JsonSort->new_with_options()->run();
App::PipeFilter::JsonSort implements a pipeline filter similar to sort(1), except that the -k flags describe JSON fields rather than whitespace-separated columns.
This filter currently provides a small subset of the options that sort(1) does.
This filter uses sort(1) and cut(1) to do most of its work.
The k() attribute names one or more key JSON fields to sort by. It is analogous to the -k flag for sort(1). MooseX::Getopt sets k() to the values of the -k options from the command line.
The boolean n() attribute (set by the -n command line flag) tells App::PipeFilter::JsonSort to sort numerically. It sorts lexicographically by default.
If specified, -n is passed to sort(1).
The boolean r() attribute (set by the -r command line flag) instructs App::PipeFilter::JsonSort to reverse the sort.
If spedified, -r is passed to sort(1).
encode_output() prepeands JSON records with the values of their key fields in tab-separated form. This output will be piped through sort(1) to do the actual sorting, and through cut(1) to remove the tab-separated sorting columns.
open_output() opens output through sort(1) and cut(1). It's a huge cheat, but it works for simple cases.
You may read this module's implementation in its entirety at
perldoc -m App::PipeFilter::JsonSort
App::PipeFilter has top-level documentation including a table of contents for all the libraries and utilities included in the project.
App::PipeFilter::JsonSort is Copyright 2011 by Rocco Caputo. All rights are reserved. App::PipeFilter::JsonSort is released under the same terms as Perl itself.