Skip to content

Commit 82004e1

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 b6f991f commit 82004e1

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
@@ -1143,6 +1143,7 @@ Philip Guenther <[email protected]>
11431143
Philip Hazel <[email protected]>
11441144
Philip M. Gollucci <[email protected]>
11451145
Philip Newton <[email protected]>
1146+
Philipp Böschen <[email protected]>
11461147
Philippe Bruhat (BooK) <[email protected]>
11471148
Philippe M. Chiasson <[email protected]>
11481149
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
@@ -1017,6 +1017,7 @@ perl_destruct(pTHXx)
10171017
PL_minus_p = FALSE;
10181018
PL_minus_l = FALSE;
10191019
PL_minus_a = FALSE;
1020+
PL_minus_j = FALSE;
10201021
PL_minus_F = FALSE;
10211022
PL_doswitches = FALSE;
10221023
PL_dowarn = G_WARN_OFF;
@@ -2244,6 +2245,7 @@ S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
22442245
case 'h':
22452246
case 'i':
22462247
case 'l':
2248+
case 'j':
22472249
case 'n':
22482250
case 'p':
22492251
case 's':
@@ -3551,6 +3553,7 @@ S_usage(pTHX) /* XXX move this out into a module ? */
35513553
" -l[octnum] enable line ending processing, specifies line terminator\n"
35523554
" -[mM][-]module execute \"use/no module...\" before executing program\n"
35533555
" -n assume \"while (<>) { ... }\" loop around program\n"
3556+
" -j auto decode_json with -n or -p into $data"
35543557
" -p assume loop like -n but print line also, like sed\n"
35553558
" -s enable rudimentary parsing for switches after programfile\n"
35563559
" -S look for programfile using PATH environment variable\n",
@@ -3858,6 +3861,10 @@ Perl_moreswitches(pTHX_ const char *s)
38583861
else
38593862
croak("No directory specified for -I");
38603863
return s;
3864+
case 'j':
3865+
PL_minus_j = TRUE;
3866+
s++;
3867+
return s;
38613868
case 'l':
38623869
PL_minus_l = TRUE;
38633870
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> ]>
@@ -663,6 +663,16 @@ X<-I> X<@INC>
663663
Directories specified by B<-I> are prepended to the search path for
664664
modules (C<@INC>).
665665

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

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
@@ -15827,6 +15827,7 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,
1582715827
PL_minus_p = proto_perl->Iminus_p;
1582815828
PL_minus_l = proto_perl->Iminus_l;
1582915829
PL_minus_a = proto_perl->Iminus_a;
15830+
PL_minus_j = proto_perl->Iminus_j;
1583015831
PL_minus_E = proto_perl->Iminus_E;
1583115832
PL_minus_F = proto_perl->Iminus_F;
1583215833
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
@@ -1527,8 +1527,13 @@ Perl_lex_next_chunk(pTHX_ U32 flags)
15271527
PL_parser->rsfp = NULL;
15281528
PL_parser->in_pod = PL_parser->filtered = 0;
15291529
if (!PL_in_eval && PL_minus_p) {
1530-
sv_catpvs(linestr,
1531-
/*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1530+
if (PL_minus_j) {
1531+
sv_catpvs(linestr,
1532+
/*{*/";}continue{print JSON::PP::encode_json($_) or die qq(-p destination: $!\\n);}");
1533+
} else {
1534+
sv_catpvs(linestr,
1535+
/*{*/";}continue{print or die qq(-p destination: $!\\n);}");
1536+
};
15321537
PL_minus_n = PL_minus_p = 0;
15331538
} else if (!PL_in_eval && PL_minus_n) {
15341539
sv_catpvs(linestr, /*{*/";}");
@@ -9208,6 +9213,10 @@ yyl_try(pTHX_ char *s)
92089213
sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
92099214
if (PL_minus_l)
92109215
sv_catpvs(PL_linestr,"chomp;");
9216+
if (PL_minus_j) {
9217+
sv_catpvs(PL_linestr,"use JSON::PP ();");
9218+
sv_catpvs(PL_linestr,"$_ = JSON::PP::decode_json($_);");
9219+
}
92119220
if (PL_minus_a) {
92129221
if (PL_minus_F) {
92139222
if ( ( *PL_splitstr == '/'

0 commit comments

Comments
 (0)