App::PipeFilter::JsonSort - a sort(1)-like filter that understands JSON fields
version 0.005
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
This class subclasses App::PipeFilter::Generic::Generic. It is customized with App::PipeFilter::Role::Reader::Sysread, App::PipeFilter::Role::Input::Json and App::PipeFilter::Role::Transform::None.
App::PipeFilter has top-level documentation including a table of contents for all the libraries and utilities included in the project.
https://rt.cpan.org/Public/Dist/Display.html?Name=App-PipeFilter
https://github.com/rcaputo/app-pipefilter
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.
To install App::PipeFilter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::PipeFilter
CPAN shell
perl -MCPAN -e shell install App::PipeFilter
For more information on module installation, please visit the detailed CPAN module installation guide.