Skip to content

Commit cc23b7a

Browse files
committed
Add a new perlrun option "-j" which allows JSON parsing in line
With this option we basically extend the already existing "-n" and "-p" flags to handle JSON using the inbuilt "JSON::PP" module. Basically we decode the STDIN from "$_" with decode_json into $_ again so you can run something like: sudo journalctl -f -o json | ./perl -njle 'if ( $_->{"_GID"} == 0 ) {print $_->{"MESSAGE"}}' To quickly filter out all root processes, this would make adhoc log analysis of most modern applications a more convenient.
1 parent 4acc9fb commit cc23b7a

File tree

9 files changed

+35
-5
lines changed

9 files changed

+35
-5
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ Philip Guenther <[email protected]>
11421142
Philip Hazel <[email protected]>
11431143
Philip M. Gollucci <[email protected]>
11441144
Philip Newton <[email protected]>
1145+
Philipp Böschen <[email protected]>
11451146
Philippe Bruhat (BooK) <[email protected]>
11461147
Philippe M. Chiasson <[email protected]>
11471148
Pierre Bogossian <[email protected]>

embedvar.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

intrpvar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ PERLVAR(I, minus_n, bool)
510510
PERLVAR(I, minus_p, bool)
511511
PERLVAR(I, minus_l, bool)
512512
PERLVAR(I, minus_a, bool)
513+
PERLVAR(I, minus_j, bool)
513514
PERLVAR(I, minus_F, bool)
514515
PERLVAR(I, doswitches, bool)
515516
PERLVAR(I, minus_E, bool)

perl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ perl_destruct(pTHXx)
10161016
PL_minus_p = FALSE;
10171017
PL_minus_l = FALSE;
10181018
PL_minus_a = FALSE;
1019+
PL_minus_j = FALSE;
10191020
PL_minus_F = FALSE;
10201021
PL_doswitches = FALSE;
10211022
PL_dowarn = G_WARN_OFF;
@@ -2245,6 +2246,7 @@ S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
22452246
case 'h':
22462247
case 'i':
22472248
case 'l':
2249+
case 'j':
22482250
case 'n':
22492251
case 'p':
22502252
case 's':
@@ -3552,6 +3554,7 @@ S_usage(pTHX) /* XXX move this out into a module ? */
35523554
" -l[octnum] enable line ending processing, specifies line terminator\n"
35533555
" -[mM][-]module execute \"use/no module...\" before executing program\n"
35543556
" -n assume \"while (<>) { ... }\" loop around program\n"
3557+
" -j auto decode_json with -n or -p into $data"
35553558
" -p assume loop like -n but print line also, like sed\n"
35563559
" -s enable rudimentary parsing for switches after programfile\n"
35573560
" -S look for programfile using PATH environment variable\n",
@@ -3859,6 +3862,10 @@ Perl_moreswitches(pTHX_ const char *s)
38593862
else
38603863
Perl_croak(aTHX_ "No directory specified for -I");
38613864
return s;
3865+
case 'j':
3866+
PL_minus_j = TRUE;
3867+
s++;
3868+
return s;
38623869
case 'l':
38633870
PL_minus_l = TRUE;
38643871
s++;

pod/perlrun.pod

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ perlrun - how to execute the Perl interpreter
77
B<perl> S<[ B<-gsTtuUWX> ]>
88
S<[ B<-h?v> ] [ B<-V>[:I<configvar>] ]>
99
S<[ B<-cw> ] [ B<-d>[B<t>][:I<debugger>] ] [ B<-D>[I<number/list>] ]>
10-
S<[ B<-pna> ] [ B<-F>I<pattern> ] [ B<-l>[I<octal>] ] [ B<-0>[I<octal/hexadecimal>] ]>
10+
S<[ B<-pnaj> ] [ B<-F>I<pattern> ] [ B<-l>[I<octal>] ] [ B<-0>[I<octal/hexadecimal>] ]>
1111
S<[ B<-I>I<dir> ] [ B<-m>[B<->]I<module> ] [ B<-M>[B<->]I<'module...'> ] [ B<-f> ]>
1212
S<[ B<-C [I<number/list>] >]>
1313
S<[ B<-S> ]>
@@ -662,6 +662,16 @@ X<-I> X<@INC>
662662
Directories specified by B<-I> are prepended to the search path for
663663
modules (C<@INC>).
664664

665+
=item B<-j>
666+
667+
enables automatic JSON decoding when used with L</-n> and decode plus
668+
printing when used with L</-p>. It basically automatically transforms
669+
C<$_> into a hashref that has been parsed from the current input using
670+
JSON::PP::decode_json.
671+
When used with L</-p> it overwrites the final print statement to first
672+
call JSON::PP::encode_json so whatever is in C<$_> will be JSON encoded
673+
before printing.
674+
665675
=item B<-l>[I<octnum>]
666676
X<-l> X<$/> X<$\>
667677

regen/embed.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/perl -w
1+
#!/usr/bin/perl -W
22
#
33
# Regenerate (overwriting only if changed):
44
#

sv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15813,6 +15813,7 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,
1581315813
PL_minus_p = proto_perl->Iminus_p;
1581415814
PL_minus_l = proto_perl->Iminus_l;
1581515815
PL_minus_a = proto_perl->Iminus_a;
15816+
PL_minus_j = proto_perl->Iminus_j;
1581615817
PL_minus_E = proto_perl->Iminus_E;
1581715818
PL_minus_F = proto_perl->Iminus_F;
1581815819
PL_doswitches = proto_perl->Idoswitches;

t/run/switches.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ is runperl(stderr => 1, prog => '#!perl -M'),
332332

333333
# Tests for switches which do not exist
334334

335-
foreach my $switch (split //, "ABbGHJjKkLNOoPQqRrYyZz123456789_")
335+
foreach my $switch (split //, "ABbGHJKkLNOoPQqRrYyZz123456789_")
336336
{
337337
local $TODO = ''; # these ones should work on VMS
338338

toke.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,8 +1564,13 @@ Perl_lex_next_chunk(pTHX_ U32 flags)
15641564
PL_parser->rsfp = NULL;
15651565
PL_parser->in_pod = PL_parser->filtered = 0;
15661566
if (!PL_in_eval && PL_minus_p) {
1567-
sv_catpvs(linestr,
1568-
/*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1567+
if (PL_minus_j) {
1568+
sv_catpvs(linestr,
1569+
/*{*/";}continue{print JSON::PP::encode_json($_) or die qq(-p destination: $!\\n);}");
1570+
} else {
1571+
sv_catpvs(linestr,
1572+
/*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1573+
};
15691574
PL_minus_n = PL_minus_p = 0;
15701575
} else if (!PL_in_eval && PL_minus_n) {
15711576
sv_catpvs(linestr, /*{*/";}");
@@ -9195,6 +9200,10 @@ yyl_try(pTHX_ char *s)
91959200
sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
91969201
if (PL_minus_l)
91979202
sv_catpvs(PL_linestr,"chomp;");
9203+
if (PL_minus_j) {
9204+
sv_catpvs(PL_linestr,"use JSON::PP ();");
9205+
sv_catpvs(PL_linestr,"$_ = JSON::PP::decode_json($_);");
9206+
}
91989207
if (PL_minus_a) {
91999208
if (PL_minus_F) {
92009209
if ( ( *PL_splitstr == '/'

0 commit comments

Comments
 (0)