Skip to content

Add a new perlrun option "-j" which allows JSON parsing in line #22718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@ Philip Guenther <[email protected]>
Philip Hazel <[email protected]>
Philip M. Gollucci <[email protected]>
Philip Newton <[email protected]>
Philipp Böschen <[email protected]>
Philippe Bruhat (BooK) <[email protected]>
Philippe M. Chiasson <[email protected]>
Pierre Bogossian <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions embedvar.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions intrpvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ PERLVAR(I, minus_n, bool)
PERLVAR(I, minus_p, bool)
PERLVAR(I, minus_l, bool)
PERLVAR(I, minus_a, bool)
PERLVAR(I, minus_j, bool)
PERLVAR(I, minus_F, bool)
PERLVAR(I, doswitches, bool)
PERLVAR(I, minus_E, bool)
Expand Down
7 changes: 7 additions & 0 deletions perl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ perl_destruct(pTHXx)
PL_minus_p = FALSE;
PL_minus_l = FALSE;
PL_minus_a = FALSE;
PL_minus_j = FALSE;
PL_minus_F = FALSE;
PL_doswitches = FALSE;
PL_dowarn = G_WARN_OFF;
Expand Down Expand Up @@ -2244,6 +2245,7 @@ S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
case 'h':
case 'i':
case 'l':
case 'j':
case 'n':
case 'p':
case 's':
Expand Down Expand Up @@ -3551,6 +3553,7 @@ S_usage(pTHX) /* XXX move this out into a module ? */
" -l[octnum] enable line ending processing, specifies line terminator\n"
" -[mM][-]module execute \"use/no module...\" before executing program\n"
" -n assume \"while (<>) { ... }\" loop around program\n"
" -j auto decode_json with -n or -p into $data"
" -p assume loop like -n but print line also, like sed\n"
" -s enable rudimentary parsing for switches after programfile\n"
" -S look for programfile using PATH environment variable\n",
Expand Down Expand Up @@ -3858,6 +3861,10 @@ Perl_moreswitches(pTHX_ const char *s)
else
croak("No directory specified for -I");
return s;
case 'j':
PL_minus_j = TRUE;
s++;
return s;
case 'l':
PL_minus_l = TRUE;
s++;
Expand Down
12 changes: 11 additions & 1 deletion pod/perlrun.pod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ perlrun - how to execute the Perl interpreter
B<perl> S<[ B<-gsTtuUWX> ]>
S<[ B<-h?v> ] [ B<-V>[:I<configvar>] ]>
S<[ B<-cw> ] [ B<-d>[B<t>][:I<debugger>] ] [ B<-D>[I<number/list>] ]>
S<[ B<-pna> ] [ B<-F>I<pattern> ] [ B<-l>[I<octal>] ] [ B<-0>[I<octal/hexadecimal>] ]>
S<[ B<-pnaj> ] [ B<-F>I<pattern> ] [ B<-l>[I<octal>] ] [ B<-0>[I<octal/hexadecimal>] ]>
S<[ B<-I>I<dir> ] [ B<-m>[B<->]I<module> ] [ B<-M>[B<->]I<'module...'> ] [ B<-f> ]>
S<[ B<-C [I<number/list>] >]>
S<[ B<-S> ]>
Expand Down Expand Up @@ -663,6 +663,16 @@ X<-I> X<@INC>
Directories specified by B<-I> are prepended to the search path for
modules (C<@INC>).

=item B<-j>

enables automatic JSON decoding when used with L</-n> and decode plus
printing when used with L</-p>. It basically automatically transforms
C<$_> into a hashref that has been parsed from the current input using
JSON::PP::decode_json.
When used with L</-p> it overwrites the final print statement to first
call JSON::PP::encode_json so whatever is in C<$_> will be JSON encoded
before printing.

=item B<-l>[I<octnum>]
X<-l> X<$/> X<$\>

Expand Down
2 changes: 1 addition & 1 deletion regen/embed.pl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/perl -w
#!/usr/bin/perl -W
#
# Regenerate (overwriting only if changed):
#
Expand Down
1 change: 1 addition & 0 deletions sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15827,6 +15827,7 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,
PL_minus_p = proto_perl->Iminus_p;
PL_minus_l = proto_perl->Iminus_l;
PL_minus_a = proto_perl->Iminus_a;
PL_minus_j = proto_perl->Iminus_j;
PL_minus_E = proto_perl->Iminus_E;
PL_minus_F = proto_perl->Iminus_F;
PL_doswitches = proto_perl->Idoswitches;
Expand Down
2 changes: 1 addition & 1 deletion t/run/switches.t
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ is runperl(stderr => 1, prog => '#!perl -M'),

# Tests for switches which do not exist

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

Expand Down
13 changes: 11 additions & 2 deletions toke.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,8 +1527,13 @@ Perl_lex_next_chunk(pTHX_ U32 flags)
PL_parser->rsfp = NULL;
PL_parser->in_pod = PL_parser->filtered = 0;
if (!PL_in_eval && PL_minus_p) {
sv_catpvs(linestr,
/*{*/";}continue{print or die qq(-p destination: $!\\n);}");
if (PL_minus_j) {
sv_catpvs(linestr,
/*{*/";}continue{print JSON::PP::encode_json($_) or die qq(-p destination: $!\\n);}");
} else {
sv_catpvs(linestr,
/*{*/";}continue{print or die qq(-p destination: $!\\n);}");
};
PL_minus_n = PL_minus_p = 0;
} else if (!PL_in_eval && PL_minus_n) {
sv_catpvs(linestr, /*{*/";}");
Expand Down Expand Up @@ -9208,6 +9213,10 @@ yyl_try(pTHX_ char *s)
sv_catpvs(PL_linestr, "LINE: while (<>) {"/*}*/);
if (PL_minus_l)
sv_catpvs(PL_linestr,"chomp;");
if (PL_minus_j) {
sv_catpvs(PL_linestr,"use JSON::PP ();");
sv_catpvs(PL_linestr,"$_ = JSON::PP::decode_json($_);");
}
if (PL_minus_a) {
if (PL_minus_F) {
if ( ( *PL_splitstr == '/'
Expand Down
Loading