Skip to content

Commit 9e68d18

Browse files
committed
Add a PPC for "-j" perlrun option
This proposes a new perlrun option flag that handles JSON input.
1 parent 44d0647 commit 9e68d18

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

ppcs/ppc0034-j-perlrun-opt.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# A short and accurate title
2+
3+
## Preamble
4+
5+
Author: Philipp Böschen <[email protected]>
6+
Sponsor:
7+
ID: 0034
8+
Status: Draft
9+
10+
## Abstract
11+
12+
This PPC suggests the implementation of a new perlrun option `-j` that works in conjunction with `-p` or `-n` to automatically feed the stdin that is parsed into `JSON::PP::decode_json($_);`
13+
A working example can be found at: https://github.com/Perl/perl5/pull/22718
14+
15+
## Motivation
16+
17+
Ultimately this change would make Perl another step further to being extremely useful in adhoc sysadmin tasks, a role that is currently covered by, usually, the `jq` tool.
18+
I personally have found that `jq`, while useful for quick extracts, becomes very cumbersome if you have to do conditional logic, which is where `perl -jnle` would shine since usually a simple `grep` or `map` can work wonders on getting good data quickly.
19+
JSON has largely become ubiquitous with modern API handling, so I feel like it's not overly specific to let Perl "natively" handle it.
20+
21+
As the [PRs](https://github.com/Perl/perl5/pull/22718) discussion shows, there are of course alternatives, but none of them seem to be as accessible as a `-j` flag, especially if we are thinking of where `jq` is often applied:
22+
- On cloud-init scripts where Perl is present but not really considered
23+
- In a bare bones debugging environment it gets often pulled in, Perl would already be there
24+
25+
## Rationale
26+
27+
I have tested the aformentioned [PR](https://github.com/Perl/perl5/pull/22718) for a while at work and privately and found it quite useful in place of `jq`. It has one less dependency on my existing debugging kit and performs reasonably well.
28+
29+
Any other proposed solutions always come with the catch that now I have to bring said solution along with me to the debugging target, be it a source filter or just a whole different tool written in Perl itself.
30+
31+
There are some thoughts on the "-p" interaction for things that ingest JSON and don't want to produce a new JSON from it, I personally am unsure of what the ideal here would be right now.
32+
33+
## Specification
34+
35+
This would be an extension of https://perldoc.perl.org/perlrun.
36+
```pod
37+
=item -j
38+
39+
causes Perl to consume the lines that "-n" provides to be fed into C<JSON::PP::decode_json($_);> to give access to line delimited JSON feeds as Perl hashes directly. This will fail with the usual errors from C<JSON::PP::decode_json> if the line that is read is not valid JSON.
40+
When used with both "-n" and -p", "-j" calls C<print JSON::PP::encode_json($_);> to properly print out JSON again.
41+
```
42+
## Backwards Compatibility
43+
44+
This change can be backported into all versions that have a working `JSON::PP` module.
45+
46+
## Security Implications
47+
48+
This would open up a similar security exposure as any perl scripts have that use `JSON::PP`. Of course if this feature gets widely adopted into server bootstraps the criticality of `JSON::PP` becomes more vital over time.
49+
50+
## Examples
51+
52+
Structured logging is the obvious use case for this feature so you can for example force `journald` into line by line JSON mode and then parse it like so to list all root processes logs:
53+
```
54+
sudo journalctl -f -o json | ./perl -njle 'if ( $_->{"_GID"} == 0 ) {print $_->{"MESSAGE"}}'
55+
```
56+
57+
This would work similarly for most structured logging applications that have somewhat all agreed on JSON being the format of choice.
58+
59+
A quick example from parsing audit logs for Hashicorp Vault:
60+
61+
```
62+
tail -f /var/log/vault/audit.log | perl -jnle 'if ( $_->{request}{client_id} eq "some_client" ) { print "$_->{request}{operation},$_->{request}{path}\n" }'
63+
update,auth/token/create
64+
...
65+
```
66+
67+
## Prototype Implementation
68+
69+
https://github.com/Perl/perl5/pull/22718 implements the full PPC while also passing all tests.
70+
71+
## Future Scope
72+
73+
Were there any ideas or variations considered that seemed reasonable follow-ons from the initial suggestions, that we don't need to do now, but want to revisit in the future? If there are parts of the design marked as "intended for future expansion", then give an idea here of what direction is planned.
74+
75+
If that future expansion relies on extending the syntax, then ensure that the first implementation has tests to assert that the extended syntax remains a syntax error.
76+
77+
## Rejected Ideas
78+
79+
Why this solution/this syntax was better than the obvious alternatives.
80+
81+
We've seen before that there will be **F**requently **A**sked **Q**uestions.
82+
eg *Why not have different behaviour in void context?*
83+
84+
Likely the answer is in the previous discussion **somewhere**, but most people won't stop to read all the comments. It needs to be easy to find, and updated as it becomes clear which questions are common.
85+
86+
Hence it **needs** to be in the PPC itself. Without this, the PPC process as a whole won't scale.
87+
88+
## Open Issues
89+
90+
Use this to summarise any points that are still to be resolved.
91+
92+
## Copyright
93+
94+
Copyright (C) 2038, A.U. Thor.
95+
96+
This document and code and documentation within it may be used, redistributed and/or modified under the same terms as Perl itself.

0 commit comments

Comments
 (0)