| 1 | package JQ::XS; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 6 | ▶ | use 5.026003; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 6 | require XSLoader; | |||||||||||||||||
| 10 | 6 | ▶ | use Exporter 'import'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | # process() blesses JSON booleans into JSON::PP::Boolean, so that class needs | ||||||||||||||||||
| 13 | # its overloads (numification, increment, fallback, ...) loaded. Other JSON | ||||||||||||||||||
| 14 | # modules install those operators into JSON::PP::Boolean themselves -- | ||||||||||||||||||
| 15 | # Cpanel::JSON::XS does, and JSON::XS does it through Types::Serialiser -- and | ||||||||||||||||||
| 16 | # loading JSON::PP::Boolean on top of them redefines the operators, which warns | ||||||||||||||||||
| 17 | # under -w. So only load it when nobody has set the class up yet; "((" is the | ||||||||||||||||||
| 18 | # marker overload.pm installs in every overloaded class. | ||||||||||||||||||
| 19 | # | ||||||||||||||||||
| 20 | # Load it as JSON::PP rather than JSON::PP::Boolean on its own: a later | ||||||||||||||||||
| 21 | # Cpanel::JSON::XS reads $JSON::PP::VERSION to decide whether the operators | ||||||||||||||||||
| 22 | # need installing, and warns about it being undefined if it finds the boolean | ||||||||||||||||||
| 23 | # class loaded without its parent. | ||||||||||||||||||
| 24 | BEGIN { | ||||||||||||||||||
| 25 | 6 | ▶ | require JSON::PP unless JSON::PP::Boolean->can('(('); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||
| 26 | } | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | 6 | our $VERSION = '2.03'; | |||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 6 | XSLoader::load('JQ::XS', $VERSION); | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | # Constants | ||||||||||||||||||
| 33 | sub JQ_DEBUG_TRACE () { 1 } | ||||||||||||||||||
| 34 | sub JQ_DEBUG_TRACE_DETAIL () { 2 } | ||||||||||||||||||
| 35 | sub JQ_DEBUG_TRACE_ALL () { 3 } | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | # jv_dump_string flags, from jq's jv.h. Not exported: the output options | ||||||||||||||||||
| 38 | # below are the supported way to ask for these, and the bit layout is jq's | ||||||||||||||||||
| 39 | # private business. | ||||||||||||||||||
| 40 | sub JV_PRINT_PRETTY () { 1 } | ||||||||||||||||||
| 41 | sub JV_PRINT_ASCII () { 2 } | ||||||||||||||||||
| 42 | sub JV_PRINT_COLOR () { 4 } | ||||||||||||||||||
| 43 | sub JV_PRINT_SORTED () { 8 } | ||||||||||||||||||
| 44 | sub JV_PRINT_TAB () { 64 } | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 6 | our @EXPORT_OK = qw( | |||||||||||||||||
| 47 | JQ_DEBUG_TRACE | ||||||||||||||||||
| 48 | JQ_DEBUG_TRACE_DETAIL | ||||||||||||||||||
| 49 | JQ_DEBUG_TRACE_ALL | ||||||||||||||||||
| 50 | jq_version | ||||||||||||||||||
| 51 | features | ||||||||||||||||||
| 52 | set_colors | ||||||||||||||||||
| 53 | parse_json | ||||||||||||||||||
| 54 | parse_json_stream | ||||||||||||||||||
| 55 | to_json | ||||||||||||||||||
| 56 | ); | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | JQ::XS - Perl wrapper for libjq | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | =head1 SYNOPSIS | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | use JQ::XS; | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | my $jq = JQ::XS->new('.foo[] | select(. > 2)'); | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | # Perl data interface | ||||||||||||||||||
| 69 | my @results = $jq->process({ foo => [1, 3, 5] }); | ||||||||||||||||||
| 70 | # Returns: (3, 5) | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | # JSON text interface | ||||||||||||||||||
| 73 | my @out = $jq->process_json('{"foo":[1,3,5]}'); | ||||||||||||||||||
| 74 | # Returns: ('3', '5') | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | # Named arguments, as jq's --arg/--argjson give you | ||||||||||||||||||
| 77 | my $greet = JQ::XS->new('"\($greeting), \(.name)!"', | ||||||||||||||||||
| 78 | vars => { greeting => 'Hello' }); | ||||||||||||||||||
| 79 | my ($msg) = $greet->process({ name => 'Alice' }); # "Hello, Alice!" | ||||||||||||||||||
| 80 | |||||||||||||||||||
| 81 | # Output formatting, as jq's -S/--tab/-r give you | ||||||||||||||||||
| 82 | my $pretty = JQ::XS->new('.', tab => 1, sort_keys => 1); | ||||||||||||||||||
| 83 | print $pretty->process_json('{"b":1,"a":2}'), "\n"; | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | # Refuse filters that pull in modules from disk | ||||||||||||||||||
| 86 | my $safe = do { | ||||||||||||||||||
| 87 | delete local $ENV{HOME}; # ...including the ~/.jq jq adds on its own | ||||||||||||||||||
| 88 | JQ::XS->new($untrusted, allow_includes => 0); | ||||||||||||||||||
| 89 | }; | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | # The debug, stderr and input/inputs builtins | ||||||||||||||||||
| 92 | my $sum = JQ::XS->new('[., inputs] | add', | ||||||||||||||||||
| 93 | inputs => [2, 3], | ||||||||||||||||||
| 94 | debug => sub { warn "jq: $_[0]\n" }); | ||||||||||||||||||
| 95 | my ($total) = $sum->process(1); # 6 | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | # Get the program source | ||||||||||||||||||
| 98 | my $prog = $jq->program; | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | JQ::XS provides a clean object-oriented wrapper around libjq, the C library | ||||||||||||||||||
| 103 | behind the jq command-line tool. It allows you to: | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | - Compile and execute jq filter programs | ||||||||||||||||||
| 106 | - Process Perl data structures (hashes, arrays, numbers, strings) | ||||||||||||||||||
| 107 | - Process JSON text | ||||||||||||||||||
| 108 | - Pass named and positional arguments to a filter | ||||||||||||||||||
| 109 | - Format JSON output the way the jq command line does | ||||||||||||||||||
| 110 | - Serve the C<input>, C<inputs>, C<debug> and C<stderr> builtins from Perl | ||||||||||||||||||
| 111 | - Restrict what a filter may load from disk | ||||||||||||||||||
| 112 | - Handle errors gracefully with Perl exceptions (croak) | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | =head1 METHODS | ||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | =head2 new($program, %options) | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | Creates a new JQ::XS object by compiling the given jq filter program. | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | my $jq = JQ::XS->new('.foo'); | ||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | Croaks with an error message if the program fails to compile. | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | The remaining arguments are name/value pairs. Every one of them has a | ||||||||||||||||||
| 125 | matching method, so anything that can be passed here can also be changed | ||||||||||||||||||
| 126 | later; the exceptions are C<vars>, C<args>, C<attrs>, C<library_paths> and | ||||||||||||||||||
| 127 | C<allow_includes>, which take effect while the program is being compiled and | ||||||||||||||||||
| 128 | so have to be given up front. | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | =over 4 | ||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | =item vars => \%hash | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | Named arguments, as C<jq --arg> and C<jq --argjson> pass them. Each key | ||||||||||||||||||
| 135 | becomes a C<$variable> in the program, and the values are arbitrary Perl data | ||||||||||||||||||
| 136 | converted the same way C<process()> converts its input. | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | my $jq = JQ::XS->new('.[] | select(.dept == $dept)', | ||||||||||||||||||
| 139 | vars => { dept => 'sales' }); | ||||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | =item args => \@list | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | Positional arguments, as C<jq --args> passes them. They are available to the | ||||||||||||||||||
| 144 | program as C<$ARGS.positional>. | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | C<$ARGS> is always defined, whether or not either option was given: | ||||||||||||||||||
| 147 | C<$ARGS.named> holds C<vars> and C<$ARGS.positional> holds C<args>, exactly | ||||||||||||||||||
| 148 | as on the jq command line. | ||||||||||||||||||
| 149 | |||||||||||||||||||
| 150 | =item allow_includes => 0 | ||||||||||||||||||
| 151 | |||||||||||||||||||
| 152 | Reject the program if it contains an C<include> or C<import> directive, | ||||||||||||||||||
| 153 | rather than compiling it and letting it read C<.jq> files from disk. Defaults | ||||||||||||||||||
| 154 | to true, i.e. modules work as they always have. It does not stop jq importing | ||||||||||||||||||
| 155 | F<~/.jq> -- see L</RESTRICTING MODULE LOADING> for that. | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | =item library_paths => \@dirs | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | Where C<include> and C<import> look for modules -- jq's C<-L> option, and its | ||||||||||||||||||
| 160 | C<JQ_LIBRARY_PATH> attribute. See L</RESTRICTING MODULE LOADING> for what | ||||||||||||||||||
| 161 | this does and does not prevent. | ||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | Defaults to empty, which is not what the jq command line defaults to: jq adds | ||||||||||||||||||
| 164 | two directories beside its own binary, and a library embedded in someone | ||||||||||||||||||
| 165 | else's program has no business reading those uninvited. It does not keep | ||||||||||||||||||
| 166 | F<~/.jq> out, which jq imports without consulting the library path at all -- | ||||||||||||||||||
| 167 | see L</RESTRICTING MODULE LOADING>. | ||||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | =item attrs => \%hash | ||||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | jq attributes to set before the program is compiled, for anything | ||||||||||||||||||
| 172 | C<library_paths> does not cover: C<JQ_ORIGIN>, C<PROGRAM_ORIGIN>, and | ||||||||||||||||||
| 173 | C<JQ_LIBRARY_PATH> itself. See L<attr()|/attr($name)> and | ||||||||||||||||||
| 174 | L<set_attr()|/set_attr($name, $value)>. | ||||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | =item flags => $flags | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | Flags for each run of the program, OR'd together from L</CONSTANTS>. Only | ||||||||||||||||||
| 179 | jq's execution tracing lives here. See L<flags()|/flags($flags)>. | ||||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | =item die_on_halt_error => 1 | ||||||||||||||||||
| 182 | |||||||||||||||||||
| 183 | Turn a filter's C<halt_error> into a Perl exception. See | ||||||||||||||||||
| 184 | L<die_on_halt_error()|/die_on_halt_error($bool)>. | ||||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | =item debug => \&code | ||||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | =item stderr => \&code | ||||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | =item inputs => \&code | \@values | ||||||||||||||||||
| 191 | |||||||||||||||||||
| 192 | Perl code behind the C<debug>, C<stderr> and C<input>/C<inputs> builtins, | ||||||||||||||||||
| 193 | which do nothing at all otherwise. See | ||||||||||||||||||
| 194 | L<set_debug_cb()|/set_debug_cb(\&code)>, | ||||||||||||||||||
| 195 | L<set_stderr_cb()|/set_stderr_cb(\&code)> and | ||||||||||||||||||
| 196 | L<set_inputs()|/set_inputs($source)>. | ||||||||||||||||||
| 197 | |||||||||||||||||||
| 198 | =item pretty, indent, tab, sort_keys, ascii, color, raw | ||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | How C<process_json()> formats its output. See | ||||||||||||||||||
| 201 | L<set_output()|/set_output(%options)>. | ||||||||||||||||||
| 202 | |||||||||||||||||||
| 203 | =back | ||||||||||||||||||
| 204 | |||||||||||||||||||
| 205 | =head2 process($data) | ||||||||||||||||||
| 206 | |||||||||||||||||||
| 207 | Processes Perl data through the compiled jq filter. Takes a Perl scalar | ||||||||||||||||||
| 208 | (which can be a reference to a hash or array) and returns a list of | ||||||||||||||||||
| 209 | results. Each result is a Perl data structure. | ||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | my @results = $jq->process({ name => 'Alice' }); | ||||||||||||||||||
| 212 | |||||||||||||||||||
| 213 | In scalar context, returns an arrayref of results. | ||||||||||||||||||
| 214 | |||||||||||||||||||
| 215 | my $results_ref = scalar($jq->process($data)); | ||||||||||||||||||
| 216 | |||||||||||||||||||
| 217 | Croaks if the jq filter produces a runtime error or if the processing fails. | ||||||||||||||||||
| 218 | |||||||||||||||||||
| 219 | =head2 Boolean handling | ||||||||||||||||||
| 220 | |||||||||||||||||||
| 221 | JSON booleans returned by a filter become L<JSON::PP::Boolean> objects, | ||||||||||||||||||
| 222 | which behave as true/false in boolean context and stringify to C<1> and | ||||||||||||||||||
| 223 | C<0>. They compare equal to the C<JSON::PP::true> and C<JSON::PP::false> | ||||||||||||||||||
| 224 | constants. | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | my ($is_big) = JQ::XS->new('. > 2')->process(5); # JSON::PP::true | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | On input, the following are converted to JSON C<true>/C<false>: | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | =over 4 | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | =item * L<JSON::PP::Boolean>, C<Types::Serialiser::Boolean>, or L<boolean> | ||||||||||||||||||
| 233 | objects (by their truth value) | ||||||||||||||||||
| 234 | |||||||||||||||||||
| 235 | =item * unblessed references to a plain scalar, e.g. C<\1> and C<\0> | ||||||||||||||||||
| 236 | |||||||||||||||||||
| 237 | =item * Perl's native boolean values, i.e. the results of comparison and | ||||||||||||||||||
| 238 | logical operators and of C<builtin::true>/C<builtin::false> | ||||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | $jq->process($x > $y); # jq sees true or false, not 1 or "" | ||||||||||||||||||
| 241 | |||||||||||||||||||
| 242 | On perls before 5.36 this only works for a boolean passed directly to | ||||||||||||||||||
| 243 | C<process()>; a copy (e.g. stored in a hash or array first) loses its | ||||||||||||||||||
| 244 | boolean identity and is treated as an ordinary number/string. On perl | ||||||||||||||||||
| 245 | 5.36 and later, copies keep their boolean flag and are recognized | ||||||||||||||||||
| 246 | anywhere in the structure. | ||||||||||||||||||
| 247 | |||||||||||||||||||
| 248 | =back | ||||||||||||||||||
| 249 | |||||||||||||||||||
| 250 | =head2 process_json($json_text, %options) | ||||||||||||||||||
| 251 | |||||||||||||||||||
| 252 | Like process, but takes JSON text as input and returns a list of JSON | ||||||||||||||||||
| 253 | strings (one for each output). | ||||||||||||||||||
| 254 | |||||||||||||||||||
| 255 | my @json_out = $jq->process_json('{"name":"Alice"}'); | ||||||||||||||||||
| 256 | |||||||||||||||||||
| 257 | Croaks if the JSON input is invalid or if the jq filter produces a runtime | ||||||||||||||||||
| 258 | error. | ||||||||||||||||||
| 259 | |||||||||||||||||||
| 260 | Any options given here are merged over the object's own output options for | ||||||||||||||||||
| 261 | this one call, and are the same ones L<set_output()|/set_output(%options)> | ||||||||||||||||||
| 262 | takes: | ||||||||||||||||||
| 263 | |||||||||||||||||||
| 264 | my @pretty = $jq->process_json($json, indent => 4, sort_keys => 1); | ||||||||||||||||||
| 265 | |||||||||||||||||||
| 266 | =head2 program() | ||||||||||||||||||
| 267 | |||||||||||||||||||
| 268 | Returns the source code of the compiled jq filter program. | ||||||||||||||||||
| 269 | |||||||||||||||||||
| 270 | my $src = $jq->program; | ||||||||||||||||||
| 271 | |||||||||||||||||||
| 272 | =head1 OUTPUT FORMATTING | ||||||||||||||||||
| 273 | |||||||||||||||||||
| 274 | =head2 set_output(%options) | ||||||||||||||||||
| 275 | |||||||||||||||||||
| 276 | Sets how C<process_json()> formats its results, and returns the object. | ||||||||||||||||||
| 277 | Options not mentioned are reset to their defaults, so this replaces the | ||||||||||||||||||
| 278 | current settings rather than adding to them; to change one and keep the rest, | ||||||||||||||||||
| 279 | merge L</output_options()> yourself, or pass the option to C<process_json()> | ||||||||||||||||||
| 280 | for a single call. | ||||||||||||||||||
| 281 | |||||||||||||||||||
| 282 | $jq->set_output(indent => 2, sort_keys => 1); | ||||||||||||||||||
| 283 | |||||||||||||||||||
| 284 | Each option corresponds to a jq command-line switch: | ||||||||||||||||||
| 285 | |||||||||||||||||||
| 286 | =over 4 | ||||||||||||||||||
| 287 | |||||||||||||||||||
| 288 | =item pretty => 1 | ||||||||||||||||||
| 289 | |||||||||||||||||||
| 290 | Indent output by two spaces, one value per line, as jq does by default. The | ||||||||||||||||||
| 291 | JQ::XS default is jq's C<-c>: everything on one line. | ||||||||||||||||||
| 292 | |||||||||||||||||||
| 293 | =item indent => $n | ||||||||||||||||||
| 294 | |||||||||||||||||||
| 295 | Indent by C<$n> spaces, C<$n> being 0 to 7 -- jq's C<--indent n>. As in jq, | ||||||||||||||||||
| 296 | C<indent =E<gt> 0> still puts one value per line, just without indentation; | ||||||||||||||||||
| 297 | it is not the same as compact output. | ||||||||||||||||||
| 298 | |||||||||||||||||||
| 299 | =item tab => 1 | ||||||||||||||||||
| 300 | |||||||||||||||||||
| 301 | Indent with tabs -- jq's C<--tab>. Wins over C<indent> and C<pretty>, as it | ||||||||||||||||||
| 302 | does in jq. | ||||||||||||||||||
| 303 | |||||||||||||||||||
| 304 | =item sort_keys => 1 | ||||||||||||||||||
| 305 | |||||||||||||||||||
| 306 | Emit object keys in sorted order -- jq's C<-S>. | ||||||||||||||||||
| 307 | |||||||||||||||||||
| 308 | =item ascii => 1 | ||||||||||||||||||
| 309 | |||||||||||||||||||
| 310 | Escape all non-ASCII characters -- jq's C<-a>. | ||||||||||||||||||
| 311 | |||||||||||||||||||
| 312 | =item color => 1 | ||||||||||||||||||
| 313 | |||||||||||||||||||
| 314 | Colorize the output with ANSI escapes -- jq's C<-C>. | ||||||||||||||||||
| 315 | L<set_colors()|/set_colors($spec)> chooses the colors. | ||||||||||||||||||
| 316 | |||||||||||||||||||
| 317 | =item raw => 1 | ||||||||||||||||||
| 318 | |||||||||||||||||||
| 319 | Emit a result that is a JSON string as the string itself, without quotes or | ||||||||||||||||||
| 320 | escapes -- jq's C<-r>. Results of every other type are still JSON. | ||||||||||||||||||
| 321 | |||||||||||||||||||
| 322 | Note that C<raw> with C<ascii> behaves as C<jq -r -a> does, which is to say | ||||||||||||||||||
| 323 | that C<ascii> wins and strings come out quoted and escaped. | ||||||||||||||||||
| 324 | |||||||||||||||||||
| 325 | =back | ||||||||||||||||||
| 326 | |||||||||||||||||||
| 327 | =head2 output_options() | ||||||||||||||||||
| 328 | |||||||||||||||||||
| 329 | Returns a hashref copy of the options L<set_output()|/set_output(%options)> | ||||||||||||||||||
| 330 | was last given. | ||||||||||||||||||
| 331 | |||||||||||||||||||
| 332 | =head1 HALT AND EXIT | ||||||||||||||||||
| 333 | |||||||||||||||||||
| 334 | A filter can stop itself with jq's C<halt> or C<halt_error>. Neither is an | ||||||||||||||||||
| 335 | error as far as C<process()> is concerned -- results produced before the halt | ||||||||||||||||||
| 336 | are still returned -- so these three methods, all of which describe the most | ||||||||||||||||||
| 337 | recent run, are how you find out that it happened. | ||||||||||||||||||
| 338 | |||||||||||||||||||
| 339 | =head2 halted() | ||||||||||||||||||
| 340 | |||||||||||||||||||
| 341 | True if the last run ended in C<halt> or C<halt_error>. | ||||||||||||||||||
| 342 | |||||||||||||||||||
| 343 | =head2 exit_code() | ||||||||||||||||||
| 344 | |||||||||||||||||||
| 345 | The exit code the filter halted with, or C<undef> if it did not halt (or | ||||||||||||||||||
| 346 | halted without one). This is what the jq command line would have exited with. | ||||||||||||||||||
| 347 | |||||||||||||||||||
| 348 | =head2 error_message() | ||||||||||||||||||
| 349 | |||||||||||||||||||
| 350 | The message C<halt_error> was given, or C<undef>. Usually a string, but | ||||||||||||||||||
| 351 | C<halt_error> accepts any JSON value and this returns it converted like any | ||||||||||||||||||
| 352 | other result. | ||||||||||||||||||
| 353 | |||||||||||||||||||
| 354 | =head2 die_on_halt_error($bool) | ||||||||||||||||||
| 355 | |||||||||||||||||||
| 356 | Sets whether C<halt_error> raises a Perl exception carrying its message, and | ||||||||||||||||||
| 357 | returns the previous setting. Off by default, which is why the message would | ||||||||||||||||||
| 358 | otherwise be silently dropped -- there is no stderr for it to go to. Plain | ||||||||||||||||||
| 359 | C<halt> never raises, having no message; use L</halted()> for that. | ||||||||||||||||||
| 360 | |||||||||||||||||||
| 361 | $jq->die_on_halt_error(1); | ||||||||||||||||||
| 362 | my @out = eval { $jq->process($data) }; | ||||||||||||||||||
| 363 | warn "filter gave up: $@" if $@; | ||||||||||||||||||
| 364 | |||||||||||||||||||
| 365 | =head1 CALLBACKS | ||||||||||||||||||
| 366 | |||||||||||||||||||
| 367 | libjq leaves the C<debug>, C<stderr>, C<input> and C<inputs> builtins | ||||||||||||||||||
| 368 | unconnected, so until you give them somewhere to go, C<debug> and C<stderr> | ||||||||||||||||||
| 369 | discard their input and C<inputs> produces nothing. | ||||||||||||||||||
| 370 | |||||||||||||||||||
| 371 | An exception thrown by a callback is not lost, but it cannot be raised from | ||||||||||||||||||
| 372 | where it happens either -- that would abandon a jq program mid-run. A C<debug> | ||||||||||||||||||
| 373 | or C<stderr> exception is re-raised by C<process()> once the run has finished; | ||||||||||||||||||
| 374 | an C<inputs> exception becomes a jq error, which the filter's own C<try>/ | ||||||||||||||||||
| 375 | C<catch> can see, and which otherwise surfaces as the run's runtime error. | ||||||||||||||||||
| 376 | |||||||||||||||||||
| 377 | =head2 set_debug_cb(\&code) | ||||||||||||||||||
| 378 | |||||||||||||||||||
| 379 | Sets the code called for each C<debug> in the filter, with the value being | ||||||||||||||||||
| 380 | debugged as its only argument. Pass no argument, or C<undef>, to disconnect it | ||||||||||||||||||
| 381 | again. Returns nothing. | ||||||||||||||||||
| 382 | |||||||||||||||||||
| 383 | $jq->set_debug_cb(sub { warn 'DEBUG: ' . to_json($_[0]) . "\n" }); | ||||||||||||||||||
| 384 | |||||||||||||||||||
| 385 | =head2 set_stderr_cb(\&code) | ||||||||||||||||||
| 386 | |||||||||||||||||||
| 387 | The same, for the C<stderr> builtin. | ||||||||||||||||||
| 388 | |||||||||||||||||||
| 389 | jq grew C<jq_set_stderr_cb> in 1.7, so under C<JQ_SYSTEM=1> against an older | ||||||||||||||||||
| 390 | libjq this croaks rather than silently doing nothing, and | ||||||||||||||||||
| 391 | L<features()|/features()> reports C<stderr_cb> as false. The vendored build | ||||||||||||||||||
| 392 | always has it. | ||||||||||||||||||
| 393 | |||||||||||||||||||
| 394 | =head2 set_inputs($source) | ||||||||||||||||||
| 395 | |||||||||||||||||||
| 396 | Connects the C<input> and C<inputs> builtins to C<$source>, and returns the | ||||||||||||||||||
| 397 | object. C<$source> is either an arrayref, whose elements are handed out in | ||||||||||||||||||
| 398 | order, or a code ref called once per value: | ||||||||||||||||||
| 399 | |||||||||||||||||||
| 400 | $jq->set_inputs(sub { my $line = <$fh>; defined $line ? $line : () }); | ||||||||||||||||||
| 401 | |||||||||||||||||||
| 402 | Returning an empty list ends the stream. Returning C<undef> does not: that is | ||||||||||||||||||
| 403 | one more value, JSON C<null>. Pass C<undef> as C<$source> to disconnect. | ||||||||||||||||||
| 404 | |||||||||||||||||||
| 405 | =head1 RESTRICTING MODULE LOADING | ||||||||||||||||||
| 406 | |||||||||||||||||||
| 407 | A jq program can pull definitions in from C<.jq> files on disk: | ||||||||||||||||||
| 408 | |||||||||||||||||||
| 409 | include "foo"; # ./foo.jq, or a library path | ||||||||||||||||||
| 410 | import "bar" as bar {search:"/x"}; # /x/bar.jq | ||||||||||||||||||
| 411 | |||||||||||||||||||
| 412 | Which is worth thinking about if the programs you compile come from somewhere | ||||||||||||||||||
| 413 | you do not control. | ||||||||||||||||||
| 414 | |||||||||||||||||||
| 415 | A definition gets in from disk two ways -- a directive in the program, or the | ||||||||||||||||||
| 416 | F<~/.jq> jq imports on its own -- and they need different answers. Both happen | ||||||||||||||||||
| 417 | while the program is being compiled, which is to say inside C<new()>: nothing | ||||||||||||||||||
| 418 | is loaded later, and nothing you change on an object afterwards unloads what | ||||||||||||||||||
| 419 | it compiled with. | ||||||||||||||||||
| 420 | |||||||||||||||||||
| 421 | =head2 Directives in the program | ||||||||||||||||||
| 422 | |||||||||||||||||||
| 423 | C<allow_includes =E<gt> 0> rejects any program containing one of these | ||||||||||||||||||
| 424 | directives, at compile time, before jq has looked at the filesystem: | ||||||||||||||||||
| 425 | |||||||||||||||||||
| 426 | my $jq = JQ::XS->new($untrusted, allow_includes => 0); # croaks on include | ||||||||||||||||||
| 427 | |||||||||||||||||||
| 428 | This is decided from the program text, but it is exact rather than a guess: | ||||||||||||||||||
| 429 | jq's grammar only accepts C<include> and C<import> in the program prologue, so | ||||||||||||||||||
| 430 | that is the only place they can be, and legal programs that merely use the | ||||||||||||||||||
| 431 | words elsewhere -- C<{include: 1}>, C<.foo.include> -- still compile. | ||||||||||||||||||
| 432 | |||||||||||||||||||
| 433 | =head2 Where those directives look | ||||||||||||||||||
| 434 | |||||||||||||||||||
| 435 | C<library_paths> does not decide whether a directive may load anything, only | ||||||||||||||||||
| 436 | where it looks, as jq's C<-L> does: | ||||||||||||||||||
| 437 | |||||||||||||||||||
| 438 | my $jq = JQ::XS->new($prog, library_paths => ['/usr/share/myapp/jq']); | ||||||||||||||||||
| 439 | |||||||||||||||||||
| 440 | but it cannot by itself stop a program from loading anything, because jq | ||||||||||||||||||
| 441 | searches the current directory when a program does not say where to look, and | ||||||||||||||||||
| 442 | a program that does say -- C<include "x" {search:"/tmp"};> -- is not searching | ||||||||||||||||||
| 443 | the library paths at all. C<allow_includes =E<gt> 0> is the one that decides | ||||||||||||||||||
| 444 | whether a program may load modules; C<library_paths> only steers the ones it | ||||||||||||||||||
| 445 | is already allowed to load. | ||||||||||||||||||
| 446 | |||||||||||||||||||
| 447 | =head2 The F<~/.jq> jq imports on its own | ||||||||||||||||||
| 448 | |||||||||||||||||||
| 449 | jq imports F<~/.jq> into every program it compiles, whether or not the program | ||||||||||||||||||
| 450 | asks for it, and neither option above prevents it: | ||||||||||||||||||
| 451 | |||||||||||||||||||
| 452 | =over 4 | ||||||||||||||||||
| 453 | |||||||||||||||||||
| 454 | =item * | ||||||||||||||||||
| 455 | |||||||||||||||||||
| 456 | C<allow_includes =E<gt> 0> does not see it. That option reads the program | ||||||||||||||||||
| 457 | text, and this import is not in the program text -- jq's linker prepends it to | ||||||||||||||||||
| 458 | every program on its way to being compiled. | ||||||||||||||||||
| 459 | |||||||||||||||||||
| 460 | =item * | ||||||||||||||||||
| 461 | |||||||||||||||||||
| 462 | C<library_paths> does not apply to it. The import carries its own search path, | ||||||||||||||||||
| 463 | the home directory, and jq consults C<JQ_LIBRARY_PATH> only for an import that | ||||||||||||||||||
| 464 | names no path of its own. An empty C<library_paths> is simply not consulted. | ||||||||||||||||||
| 465 | |||||||||||||||||||
| 466 | =back | ||||||||||||||||||
| 467 | |||||||||||||||||||
| 468 | So whatever F<~/.jq> defines is in scope for the program, and can shadow | ||||||||||||||||||
| 469 | builtins for it. The import is marked optional, so it is silent when there is | ||||||||||||||||||
| 470 | no such file, which is why this tends to go unnoticed on hosts that do not | ||||||||||||||||||
| 471 | happen to have one. | ||||||||||||||||||
| 472 | |||||||||||||||||||
| 473 | jq locates that file through C<$HOME>, and skips the import when C<$HOME> is | ||||||||||||||||||
| 474 | unset. Take C<HOME> out of the environment for the call and nothing is | ||||||||||||||||||
| 475 | imported: | ||||||||||||||||||
| 476 | |||||||||||||||||||
| 477 | my $jq = do { | ||||||||||||||||||
| 478 | delete local $ENV{HOME}; | ||||||||||||||||||
| 479 | JQ::XS->new($prog); | ||||||||||||||||||
| 480 | }; | ||||||||||||||||||
| 481 | |||||||||||||||||||
| 482 | Only C<new()> has to be inside that scope. The import is resolved as the | ||||||||||||||||||
| 483 | program is compiled and is then fixed in the compiled program, so restoring | ||||||||||||||||||
| 484 | C<HOME> before you run anything neither unloads a F<~/.jq> that was already | ||||||||||||||||||
| 485 | imported nor lets one in afterwards. Nothing else in jq reads C<$HOME>, and | ||||||||||||||||||
| 486 | unsetting it changes nothing else about how a program runs. | ||||||||||||||||||
| 487 | |||||||||||||||||||
| 488 | Use C<delete local>, not C<local $ENV{HOME} = ''>. An empty C<HOME> is still a | ||||||||||||||||||
| 489 | home directory as far as jq is concerned, one whose F<~/.jq> is F</.jq>: the | ||||||||||||||||||
| 490 | import is skipped only because that file usually does not exist, not because | ||||||||||||||||||
| 491 | jq declined to look for it. C<delete local> is what makes it not look. | ||||||||||||||||||
| 492 | |||||||||||||||||||
| 493 | On anything but Windows, C<HOME> is the only variable jq consults. On Windows | ||||||||||||||||||
| 494 | it falls back to C<USERPROFILE>, and then to C<HOMEDRIVE> and C<HOMEPATH>, | ||||||||||||||||||
| 495 | when C<HOME> is unset, so all of them have to go: | ||||||||||||||||||
| 496 | |||||||||||||||||||
| 497 | delete local @ENV{qw(HOME USERPROFILE HOMEDRIVE HOMEPATH)}; | ||||||||||||||||||
| 498 | |||||||||||||||||||
| 499 | =head2 Loading nothing at all | ||||||||||||||||||
| 500 | |||||||||||||||||||
| 501 | Together, for a program you did not write: | ||||||||||||||||||
| 502 | |||||||||||||||||||
| 503 | my $jq = do { | ||||||||||||||||||
| 504 | delete local $ENV{HOME}; | ||||||||||||||||||
| 505 | JQ::XS->new($untrusted, allow_includes => 0); | ||||||||||||||||||
| 506 | }; | ||||||||||||||||||
| 507 | |||||||||||||||||||
| 508 | C<library_paths> does not appear because it does not need to: it already | ||||||||||||||||||
| 509 | defaults to empty, and with includes refused there is nothing left to steer. | ||||||||||||||||||
| 510 | |||||||||||||||||||
| 511 | =head2 library_paths(\@dirs) | ||||||||||||||||||
| 512 | |||||||||||||||||||
| 513 | Without an argument, returns the current module search path as an arrayref. | ||||||||||||||||||
| 514 | With one, replaces it and returns the object. | ||||||||||||||||||
| 515 | |||||||||||||||||||
| 516 | Setting it after construction does not retroactively change what the compiled | ||||||||||||||||||
| 517 | program included; it is the C<modulemeta> builtin and jq's C<$__loc__> | ||||||||||||||||||
| 518 | machinery that read it later. | ||||||||||||||||||
| 519 | |||||||||||||||||||
| 520 | =head1 RESTRICTING ENVIRONMENT ACCESS | ||||||||||||||||||
| 521 | |||||||||||||||||||
| 522 | A jq program can read the process environment two different ways, and they are | ||||||||||||||||||
| 523 | independent of each other: | ||||||||||||||||||
| 524 | |||||||||||||||||||
| 525 | =over 4 | ||||||||||||||||||
| 526 | |||||||||||||||||||
| 527 | =item * | ||||||||||||||||||
| 528 | |||||||||||||||||||
| 529 | C<$ENV> is resolved while the program is compiled. jq's compiler rewrites | ||||||||||||||||||
| 530 | every reference to C<$ENV> that nothing else has already bound into a constant | ||||||||||||||||||
| 531 | built by walking the process's environment -- the same object C<jq -n '$ENV'> | ||||||||||||||||||
| 532 | prints on the command line. | ||||||||||||||||||
| 533 | |||||||||||||||||||
| 534 | =item * | ||||||||||||||||||
| 535 | |||||||||||||||||||
| 536 | C<env> is an ordinary builtin function. It reads the environment itself, | ||||||||||||||||||
| 537 | fresh, each time it runs. | ||||||||||||||||||
| 538 | |||||||||||||||||||
| 539 | =back | ||||||||||||||||||
| 540 | |||||||||||||||||||
| 541 | Neither is behind an attribute or a flag the way L</RESTRICTING MODULE | ||||||||||||||||||
| 542 | LOADING> is: libjq offers no switch for either, and JQ::XS adds no option of | ||||||||||||||||||
| 543 | its own. What follows is something you do to the program text yourself, before | ||||||||||||||||||
| 544 | it is compiled, rather than anything the module does for you. | ||||||||||||||||||
| 545 | |||||||||||||||||||
| 546 | =head2 Shadowing both | ||||||||||||||||||
| 547 | |||||||||||||||||||
| 548 | jq only substitutes the real environment for C<$ENV> when nothing has bound | ||||||||||||||||||
| 549 | that name already, and C<env> is only special until something earlier in the | ||||||||||||||||||
| 550 | program redefines it -- an ordinary C<def> shadows a builtin exactly as it | ||||||||||||||||||
| 551 | would shadow any other definition, for everything lexically after it. Put both | ||||||||||||||||||
| 552 | ahead of a program and neither name reaches the real environment: | ||||||||||||||||||
| 553 | |||||||||||||||||||
| 554 | def env: {}; | ||||||||||||||||||
| 555 | {} as $ENV | ( | ||||||||||||||||||
| 556 | ... the program ... | ||||||||||||||||||
| 557 | ) | ||||||||||||||||||
| 558 | |||||||||||||||||||
| 559 | C<env> and C<$ENV> both come back C<{}> inside the parentheses, and C<env.FOO> | ||||||||||||||||||
| 560 | / C<$ENV.FOO> come back C<null>, whatever C<FOO> actually holds in the process | ||||||||||||||||||
| 561 | running it. | ||||||||||||||||||
| 562 | |||||||||||||||||||
| 563 | =head2 Mind the closing paren | ||||||||||||||||||
| 564 | |||||||||||||||||||
| 565 | jq comments run to the end of the line, so the wrapper's own closing paren is | ||||||||||||||||||
| 566 | easy to lose inside one if the program being wrapped ends in a C<#> comment | ||||||||||||||||||
| 567 | with no newline of its own after it: | ||||||||||||||||||
| 568 | |||||||||||||||||||
| 569 | # BUG: that ')' is inside the comment, not code | ||||||||||||||||||
| 570 | my $wrapped = 'def env: {}; {} as $ENV | (' . $program . ')'; | ||||||||||||||||||
| 571 | |||||||||||||||||||
| 572 | That particular case fails safely -- jq reports a syntax error for the | ||||||||||||||||||
| 573 | now-unclosed paren rather than compiling into something unintended -- but it | ||||||||||||||||||
| 574 | is still a confusing way to make an otherwise fine program stop working. Give | ||||||||||||||||||
| 575 | the closing paren its own line instead, so a trailing comment can only ever | ||||||||||||||||||
| 576 | reach the newline before it: | ||||||||||||||||||
| 577 | |||||||||||||||||||
| 578 | my $wrapped = 'def env: {}; {} as $ENV | (' . "\n" | ||||||||||||||||||
| 579 | . $program . "\n" | ||||||||||||||||||
| 580 | . ')'; | ||||||||||||||||||
| 581 | my $jq = JQ::XS->new($wrapped, allow_includes => 0); | ||||||||||||||||||
| 582 | |||||||||||||||||||
| 583 | =head2 Programs that use import/include | ||||||||||||||||||
| 584 | |||||||||||||||||||
| 585 | This prologue is textual, so it has to come before whatever the program does, | ||||||||||||||||||
| 586 | and that only works when the program has no C<import> or C<include> directive | ||||||||||||||||||
| 587 | of its own: jq's grammar allows those only at the very start of a program, a | ||||||||||||||||||
| 588 | position this prologue has just taken for itself. Wrap a program that begins | ||||||||||||||||||
| 589 | with one and jq reports a plain syntax error -- C<unexpected import> -- rather | ||||||||||||||||||
| 590 | than the clearer "not allowed" L</RESTRICTING MODULE LOADING> gives, because | ||||||||||||||||||
| 591 | the parse never gets far enough for that check to run. | ||||||||||||||||||
| 592 | |||||||||||||||||||
| 593 | Pairing the prologue with C<allow_includes =E<gt> 0> costs nothing, then: such | ||||||||||||||||||
| 594 | a program was never going to be allowed a directive in the first place, so the | ||||||||||||||||||
| 595 | prologue rules out no case that was still open. That is the combination to | ||||||||||||||||||
| 596 | reach for when the program is one you did not write, as in the example above. | ||||||||||||||||||
| 597 | |||||||||||||||||||
| 598 | For a program you do control and want both for, put the prologue after the | ||||||||||||||||||
| 599 | directives rather than before them -- C<import "x" as m; def env: {}; {} as | ||||||||||||||||||
| 600 | $ENV | ( ... )> compiles fine. Doing the same to a program you did not write | ||||||||||||||||||
| 601 | means finding where its prologue ends, which is jq lexing rather than a | ||||||||||||||||||
| 602 | regexp, so it is worth it only when you already know the shape of what you are | ||||||||||||||||||
| 603 | wrapping. | ||||||||||||||||||
| 604 | |||||||||||||||||||
| 605 | =head2 Exposing a filtered subset instead | ||||||||||||||||||
| 606 | |||||||||||||||||||
| 607 | Rather than writing the replacement into the program text -- and having to | ||||||||||||||||||
| 608 | think about escaping whatever it contains -- pass it through | ||||||||||||||||||
| 609 | L<new()|/new($program, %options)>'s C<vars> option instead, and reference it | ||||||||||||||||||
| 610 | from the prologue: | ||||||||||||||||||
| 611 | |||||||||||||||||||
| 612 | my $jq = JQ::XS->new( | ||||||||||||||||||
| 613 | 'def env: $__jqxs_env; $__jqxs_env as $ENV | (' . "\n" | ||||||||||||||||||
| 614 | . $program . "\n" | ||||||||||||||||||
| 615 | . ')', | ||||||||||||||||||
| 616 | vars => { __jqxs_env => { PATH => $ENV{PATH} } }, # Perl's %ENV here | ||||||||||||||||||
| 617 | ); | ||||||||||||||||||
| 618 | |||||||||||||||||||
| 619 | C<env> and C<$ENV> now both come back C<{"PATH": ...}> inside the program, | ||||||||||||||||||
| 620 | with the rest of the process's environment out of reach -- a way to hand a | ||||||||||||||||||
| 621 | filter the one or two variables it legitimately needs without handing it | ||||||||||||||||||
| 622 | everything. | ||||||||||||||||||
| 623 | |||||||||||||||||||
| 624 | =head1 ATTRIBUTES | ||||||||||||||||||
| 625 | |||||||||||||||||||
| 626 | =head2 attr($name) | ||||||||||||||||||
| 627 | |||||||||||||||||||
| 628 | Returns the jq attribute C<$name>, or C<undef> if it was never set. jq keeps | ||||||||||||||||||
| 629 | C<JQ_LIBRARY_PATH>, C<JQ_ORIGIN> and C<PROGRAM_ORIGIN> here. | ||||||||||||||||||
| 630 | |||||||||||||||||||
| 631 | =head2 set_attr($name, $value) | ||||||||||||||||||
| 632 | |||||||||||||||||||
| 633 | Sets one. Attributes that affect compilation have to be set through | ||||||||||||||||||
| 634 | C<new()>'s C<attrs> option instead, since by the time you have an object the | ||||||||||||||||||
| 635 | program is already compiled. | ||||||||||||||||||
| 636 | |||||||||||||||||||
| 637 | =head1 EXECUTION FLAGS | ||||||||||||||||||
| 638 | |||||||||||||||||||
| 639 | =head2 flags($flags) | ||||||||||||||||||
| 640 | |||||||||||||||||||
| 641 | Sets the flags each run passes to jq, and returns the previous value. The | ||||||||||||||||||
| 642 | only flags jq defines are the execution traces in L</CONSTANTS>, which write | ||||||||||||||||||
| 643 | to the process's standard error. | ||||||||||||||||||
| 644 | |||||||||||||||||||
| 645 | $jq->flags(JQ_DEBUG_TRACE); | ||||||||||||||||||
| 646 | |||||||||||||||||||
| 647 | =head2 dump_disassembly($indent) | ||||||||||||||||||
| 648 | |||||||||||||||||||
| 649 | Writes the compiled program's bytecode to standard output, as jq's | ||||||||||||||||||
| 650 | C<--debug-dump-disasm> does. C<$indent> defaults to 0. | ||||||||||||||||||
| 651 | |||||||||||||||||||
| 652 | jq prints this from C, to the process's file descriptor 1 rather than through | ||||||||||||||||||
| 653 | Perl's C<STDOUT> handle, so reopening C<STDOUT> in Perl will not capture it; | ||||||||||||||||||
| 654 | redirect the file descriptor if you need to. | ||||||||||||||||||
| 655 | |||||||||||||||||||
| 656 | =head1 FUNCTIONS | ||||||||||||||||||
| 657 | |||||||||||||||||||
| 658 | These are all exportable. | ||||||||||||||||||
| 659 | |||||||||||||||||||
| 660 | =head2 jq_version() | ||||||||||||||||||
| 661 | |||||||||||||||||||
| 662 | Returns the version of the jq compiled into this build, e.g. C<'1.8.2'>, or | ||||||||||||||||||
| 663 | C<undef> if the module was built against the libjq the operating system | ||||||||||||||||||
| 664 | packages (see L</EMBEDDED JQ>). | ||||||||||||||||||
| 665 | |||||||||||||||||||
| 666 | use JQ::XS qw(jq_version); | ||||||||||||||||||
| 667 | print jq_version(); # 1.8.2 | ||||||||||||||||||
| 668 | |||||||||||||||||||
| 669 | =head2 parse_json($text) | ||||||||||||||||||
| 670 | |||||||||||||||||||
| 671 | Parses one JSON value with jq's own parser and returns it as Perl data, with | ||||||||||||||||||
| 672 | the same type mapping C<process()> uses. Croaks if the text is not a single | ||||||||||||||||||
| 673 | valid JSON value. | ||||||||||||||||||
| 674 | |||||||||||||||||||
| 675 | =head2 parse_json_stream($text) | ||||||||||||||||||
| 676 | |||||||||||||||||||
| 677 | The same, for text holding any number of JSON values one after another -- | ||||||||||||||||||
| 678 | which is what the jq command line reads from a file or a pipe, and what | ||||||||||||||||||
| 679 | C<parse_json()> rejects. Returns them as a list. | ||||||||||||||||||
| 680 | |||||||||||||||||||
| 681 | my @values = parse_json_stream("1 2 {\"a\":3}"); # (1, 2, {a=>3}) | ||||||||||||||||||
| 682 | |||||||||||||||||||
| 683 | =head2 to_json($data, %options) | ||||||||||||||||||
| 684 | |||||||||||||||||||
| 685 | Serializes Perl data to JSON with jq's own printer. Takes the formatting | ||||||||||||||||||
| 686 | options L<set_output()|/set_output(%options)> takes, apart from C<raw>: | ||||||||||||||||||
| 687 | |||||||||||||||||||
| 688 | print to_json($data, indent => 2, sort_keys => 1), "\n"; | ||||||||||||||||||
| 689 | |||||||||||||||||||
| 690 | =head2 set_colors($spec) | ||||||||||||||||||
| 691 | |||||||||||||||||||
| 692 | Sets the colors C<color =E<gt> 1> output uses, in the format of jq's | ||||||||||||||||||
| 693 | C<JQ_COLORS> environment variable -- a colon-separated list of ANSI SGR | ||||||||||||||||||
| 694 | sequences for null, false, true, numbers, strings, arrays, objects and object | ||||||||||||||||||
| 695 | keys: | ||||||||||||||||||
| 696 | |||||||||||||||||||
| 697 | set_colors('0;90:0;39:0;39:0;39:0;32:1;39:1;39:34;1'); | ||||||||||||||||||
| 698 | |||||||||||||||||||
| 699 | Returns true if the specification was understood. This is a global setting | ||||||||||||||||||
| 700 | inside libjq, not a per-object one, so it affects every JQ::XS object in the | ||||||||||||||||||
| 701 | process. | ||||||||||||||||||
| 702 | |||||||||||||||||||
| 703 | =head2 features() | ||||||||||||||||||
| 704 | |||||||||||||||||||
| 705 | Returns a hashref describing what this build of the module can do, which | ||||||||||||||||||
| 706 | under C<JQ_SYSTEM=1> depends on the libjq it was linked against: | ||||||||||||||||||
| 707 | |||||||||||||||||||
| 708 | =over 4 | ||||||||||||||||||
| 709 | |||||||||||||||||||
| 710 | =item embedded_jq | ||||||||||||||||||
| 711 | |||||||||||||||||||
| 712 | True if the jq engine is compiled into the module, false under | ||||||||||||||||||
| 713 | C<JQ_SYSTEM=1>. The same distinction L<jq_version()|/jq_version()> reports by | ||||||||||||||||||
| 714 | returning C<undef>. | ||||||||||||||||||
| 715 | |||||||||||||||||||
| 716 | =item stderr_cb | ||||||||||||||||||
| 717 | |||||||||||||||||||
| 718 | True if L<set_stderr_cb()|/set_stderr_cb(\&code)> works, i.e. if the libjq | ||||||||||||||||||
| 719 | has the C<jq_set_stderr_cb> that jq 1.7 added. | ||||||||||||||||||
| 720 | |||||||||||||||||||
| 721 | =back | ||||||||||||||||||
| 722 | |||||||||||||||||||
| 723 | =head1 CONSTANTS | ||||||||||||||||||
| 724 | |||||||||||||||||||
| 725 | The following constants are available via @EXPORT_OK: | ||||||||||||||||||
| 726 | |||||||||||||||||||
| 727 | JQ_DEBUG_TRACE - value 1 | ||||||||||||||||||
| 728 | JQ_DEBUG_TRACE_DETAIL - value 2 | ||||||||||||||||||
| 729 | JQ_DEBUG_TRACE_ALL - value 3 | ||||||||||||||||||
| 730 | |||||||||||||||||||
| 731 | They are the flags L<flags()|/flags($flags)> accepts. | ||||||||||||||||||
| 732 | |||||||||||||||||||
| 733 | =head1 EMBEDDED JQ | ||||||||||||||||||
| 734 | |||||||||||||||||||
| 735 | From version 2.00, JQ::XS does not use the libjq the operating system packages. | ||||||||||||||||||
| 736 | It ships an upstream jq release under F<vendor/>, compiles it at install time | ||||||||||||||||||
| 737 | and links it statically, so the installed module has no libjq to find at | ||||||||||||||||||
| 738 | runtime and behaves the same way on every distribution regardless of the jq | ||||||||||||||||||
| 739 | version that distribution ships. L</jq_version()> reports which jq that is. | ||||||||||||||||||
| 740 | |||||||||||||||||||
| 741 | Building needs only a C compiler, C<make> and a POSIX shell; jq's own release | ||||||||||||||||||
| 742 | tarball is self-contained, and its bundled oniguruma is built too, so the regex | ||||||||||||||||||
| 743 | builtins (C<test>, C<match>, C<capture>, C<sub>, C<gsub>, C<scan>, C<splits>) | ||||||||||||||||||
| 744 | work without an external library. | ||||||||||||||||||
| 745 | |||||||||||||||||||
| 746 | To link the OS libjq instead, configure with: | ||||||||||||||||||
| 747 | |||||||||||||||||||
| 748 | perl Makefile.PL JQ_SYSTEM=1 | ||||||||||||||||||
| 749 | |||||||||||||||||||
| 750 | in which case L</jq_version()> returns C<undef> and the filter semantics are | ||||||||||||||||||
| 751 | whatever the installed libjq implements. Everything the XS uses has been in | ||||||||||||||||||
| 752 | libjq since jq 1.5 bar one: C<jq_set_stderr_cb>, behind | ||||||||||||||||||
| 753 | L<set_stderr_cb()|/set_stderr_cb(\&code)>, arrived in 1.7, and Makefile.PL | ||||||||||||||||||
| 754 | link-probes for it -- against an older libjq the build says so and | ||||||||||||||||||
| 755 | L<features()|/features()> reports C<stderr_cb> as false. | ||||||||||||||||||
| 756 | |||||||||||||||||||
| 757 | One caveat worth knowing before choosing that build, since it cannot be | ||||||||||||||||||
| 758 | worked around from here: the jq 1.6 that RHEL 8 and Debian 11 ship corrupts | ||||||||||||||||||
| 759 | its own heap in C<jq_teardown> after a filter has called C<halt_error>, which | ||||||||||||||||||
| 760 | aborts the process when the JQ::XS object is freed. Later jq releases are | ||||||||||||||||||
| 761 | fine, and so is the embedded build. | ||||||||||||||||||
| 762 | |||||||||||||||||||
| 763 | =head1 AUTHOR | ||||||||||||||||||
| 764 | |||||||||||||||||||
| 765 | James Rouzier E<lt>rouzier@gmail.comE<gt> | ||||||||||||||||||
| 766 | |||||||||||||||||||
| 767 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||
| 768 | |||||||||||||||||||
| 769 | Copyright (C) 2026 James Rouzier | ||||||||||||||||||
| 770 | |||||||||||||||||||
| 771 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||
| 772 | it under the terms of the MIT license. See the LICENSE file included | ||||||||||||||||||
| 773 | with this distribution. | ||||||||||||||||||
| 774 | |||||||||||||||||||
| 775 | =cut | ||||||||||||||||||
| 776 | |||||||||||||||||||
| 777 | # program(), the accessors, the callback setters and DESTROY are implemented | ||||||||||||||||||
| 778 | # in XS; the object is a blessed pointer to a C struct (T_PTROBJ), not a | ||||||||||||||||||
| 779 | # hashref, which is why every option below has to be stored on the C side. | ||||||||||||||||||
| 780 | |||||||||||||||||||
| 781 | 6 | my %OUTPUT_OPTION = map { $_ => 1 } | |||||||||||||||||
| 782 | qw(pretty indent tab sort_keys ascii color raw); | ||||||||||||||||||
| 783 | |||||||||||||||||||
| 784 | 6 | my %NEW_OPTION = map { $_ => 1 } qw( | |||||||||||||||||
| 785 | vars args attrs library_paths allow_includes | ||||||||||||||||||
| 786 | flags die_on_halt_error debug stderr inputs | ||||||||||||||||||
| 787 | ); | ||||||||||||||||||
| 788 | |||||||||||||||||||
| 789 | # Compile a set of output options down to the (jv_dump_string flags, raw) | ||||||||||||||||||
| 790 | # pair the XS side works with. | ||||||||||||||||||
| 791 | sub _output_flags { | ||||||||||||||||||
| 792 | 97 | ▶ | my ($opts) = @_; | ||||||||||||||||
|
Subroutine
_output_flags: called CC=10 SCAR=23.0
| |||||||||||||||||||
| 793 | |||||||||||||||||||
| 794 | 97 | my @unknown = sort grep { !$OUTPUT_OPTION{$_} } keys %$opts; | |||||||||||||||||
| 795 | 97 | ▶ | croak 'Unknown output option: ' . join(', ', @unknown) if @unknown; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 796 | |||||||||||||||||||
| 797 | 96 | my $flags = 0; | |||||||||||||||||
| 798 | |||||||||||||||||||
| 799 | # jq's own precedence, from its main.c: --tab beats --indent, and the | ||||||||||||||||||
| 800 | # default is -c (compact) rather than the pretty output jq gives a tty. | ||||||||||||||||||
| 801 | 96 | ▶ | if ($opts->{tab}) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 802 | 2 | $flags |= JV_PRINT_TAB | JV_PRINT_PRETTY; | |||||||||||||||||
| 803 | } | ||||||||||||||||||
| 804 | elsif (defined $opts->{indent}) { | ||||||||||||||||||
| 805 | croak 'indent must be an integer from 0 to 7' | ||||||||||||||||||
| 806 | 4 | ▶ | unless $opts->{indent} =~ /\A[0-7]\z/; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 807 | # jq's JV_PRINT_INDENT_FLAGS: the width goes in bits 8-10. Zero is | ||||||||||||||||||
| 808 | # not compact, it is "one value per line, unindented", same as jq. | ||||||||||||||||||
| 809 | 3 | $flags |= JV_PRINT_PRETTY | ($opts->{indent} << 8); | |||||||||||||||||
| 810 | } | ||||||||||||||||||
| 811 | elsif ($opts->{pretty}) { | ||||||||||||||||||
| 812 | 3 | $flags |= JV_PRINT_PRETTY | (2 << 8); | |||||||||||||||||
| 813 | } | ||||||||||||||||||
| 814 | |||||||||||||||||||
| 815 | 95 | ▶ | $flags |= JV_PRINT_SORTED if $opts->{sort_keys}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 816 | 95 | ▶ | $flags |= JV_PRINT_ASCII if $opts->{ascii}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 817 | 95 | ▶ | $flags |= JV_PRINT_COLOR if $opts->{color}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 818 | |||||||||||||||||||
| 819 | 95 | ▶ | return ($flags, $opts->{raw} ? 1 : 0); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 820 | } | ||||||||||||||||||
| 821 | |||||||||||||||||||
| 822 | sub new { | ||||||||||||||||||
| 823 | 103 | ▶ | my ($class, $program, %opts) = @_; | ||||||||||||||||
|
Subroutine
new: called CC=19 SCAR=29.5
Pod
documented
| |||||||||||||||||||
| 824 | |||||||||||||||||||
| 825 | # The output options share the one flat option list, so split them out | ||||||||||||||||||
| 826 | # before checking what is left for typos. | ||||||||||||||||||
| 827 | 103 | my %output; | |||||||||||||||||
| 828 | 103 | for my $key (keys %OUTPUT_OPTION) { | |||||||||||||||||
| 829 | 721 | ▶ | $output{$key} = delete $opts{$key} if exists $opts{$key}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 830 | } | ||||||||||||||||||
| 831 | |||||||||||||||||||
| 832 | 103 | my @unknown = sort grep { !$NEW_OPTION{$_} } keys %opts; | |||||||||||||||||
| 833 | 103 | ▶ | croak 'Unknown option to JQ::XS->new: ' . join(', ', @unknown) if @unknown; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 834 | |||||||||||||||||||
| 835 | 102 | ▶ | my $vars = $opts{vars} || {}; | ||||||||||||||||
|
Condition100%2/2
$opts{'vars'} || {}
MC/DC100%1/1
$opts{'vars'} || {}
$opts{'vars'}
Truth table100%2/2
A
A= $opts{'vars'}
| |||||||||||||||||||
| 836 | 102 | ▶ | ref $vars eq 'HASH' or croak 'vars must be a hash reference'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 837 | 101 | ▶ | my $args = $opts{args} || []; | ||||||||||||||||
|
Condition100%2/2
$opts{'args'} || []
MC/DC100%1/1
$opts{'args'} || []
$opts{'args'}
Truth table100%2/2
A
A= $opts{'args'}
| |||||||||||||||||||
| 838 | 101 | ▶ | ref $args eq 'ARRAY' or croak 'args must be an array reference'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 839 | |||||||||||||||||||
| 840 | # What jq's main.c hands jq_compile_args: the named arguments, plus an | ||||||||||||||||||
| 841 | # ARGS that collects them next to the positional ones. Always passing | ||||||||||||||||||
| 842 | # ARGS is what makes $ARGS defined in every program, as it is in jq. | ||||||||||||||||||
| 843 | 101 | my %compile_args = ( | |||||||||||||||||
| 844 | %$vars, | ||||||||||||||||||
| 845 | ARGS => { named => {%$vars}, positional => [@$args] }, | ||||||||||||||||||
| 846 | ); | ||||||||||||||||||
| 847 | |||||||||||||||||||
| 848 | 101 | ▶ | my %attrs = %{ $opts{attrs} || {} }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 849 | 101 | ▶ | if (my $paths = $opts{library_paths}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 850 | 4 | ▶ | ref $paths eq 'ARRAY' | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 851 | or croak 'library_paths must be an array reference'; | ||||||||||||||||||
| 852 | 4 | $attrs{JQ_LIBRARY_PATH} = [@$paths]; | |||||||||||||||||
| 853 | } | ||||||||||||||||||
| 854 | |||||||||||||||||||
| 855 | # Always leave JQ_LIBRARY_PATH holding an array, never unset. An empty | ||||||||||||||||||
| 856 | # one is the right default for a library -- jq's command line defaults to | ||||||||||||||||||
| 857 | # searching ~/.jq, which is not somewhere a module embedded in someone | ||||||||||||||||||
| 858 | # else's program should read from uninvited -- and it also steps around a | ||||||||||||||||||
| 859 | # jq 1.6 bug that a JQ_SYSTEM=1 build can still meet: 1.6's | ||||||||||||||||||
| 860 | # jq_get_lib_dirs() hands the unset attribute straight to jv_array_concat | ||||||||||||||||||
| 861 | # without checking it, and the failed assertion aborts the process while | ||||||||||||||||||
| 862 | # any program containing an include is being compiled. | ||||||||||||||||||
| 863 | 101 | ▶ | $attrs{JQ_LIBRARY_PATH} = [] unless exists $attrs{JQ_LIBRARY_PATH}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 864 | |||||||||||||||||||
| 865 | my $allow_includes = | ||||||||||||||||||
| 866 | 101 | ▶ | exists $opts{allow_includes} ? ($opts{allow_includes} ? 1 : 0) : 1; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 867 | |||||||||||||||||||
| 868 | 101 | my $self = | |||||||||||||||||
| 869 | _new($class, $program, \%compile_args, \%attrs, $allow_includes); | ||||||||||||||||||
| 870 | |||||||||||||||||||
| 871 | # Everything from here on is settable after the fact, so it goes through | ||||||||||||||||||
| 872 | # the same methods a caller would use. | ||||||||||||||||||
| 873 | 91 | $self->set_output(%output); | |||||||||||||||||
| 874 | 90 | ▶ | $self->flags($opts{flags}) if defined $opts{flags}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 875 | 90 | ▶ | $self->die_on_halt_error(1) if $opts{die_on_halt_error}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 876 | 90 | ▶ | $self->set_debug_cb($opts{debug}) if defined $opts{debug}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 877 | 89 | ▶ | $self->set_stderr_cb($opts{stderr}) if defined $opts{stderr}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 878 | 89 | ▶ | $self->set_inputs($opts{inputs}) if defined $opts{inputs}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 879 | |||||||||||||||||||
| 880 | 88 | return $self; | |||||||||||||||||
| 881 | } | ||||||||||||||||||
| 882 | |||||||||||||||||||
| 883 | sub set_output { | ||||||||||||||||||
| 884 | 93 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||
|
Subroutine
set_output: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 885 | 93 | my ($flags, $raw) = _output_flags(\%opts); | |||||||||||||||||
| 886 | 91 | _set_output($self, \%opts, $flags, $raw); | |||||||||||||||||
| 887 | 91 | return $self; | |||||||||||||||||
| 888 | } | ||||||||||||||||||
| 889 | |||||||||||||||||||
| 890 | sub set_inputs { | ||||||||||||||||||
| 891 | 8 | ▶ | my ($self, $source) = @_; | ||||||||||||||||
|
Subroutine
set_inputs: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||
| 892 | |||||||||||||||||||
| 893 | 8 | ▶ | if (!defined $source) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 894 | 0 | $self->set_input_cb(undef); | |||||||||||||||||
| 895 | } | ||||||||||||||||||
| 896 | elsif (ref $source eq 'CODE') { | ||||||||||||||||||
| 897 | 4 | $self->set_input_cb($source); | |||||||||||||||||
| 898 | } | ||||||||||||||||||
| 899 | elsif (ref $source eq 'ARRAY') { | ||||||||||||||||||
| 900 | # One element per call, then the empty list that ends the stream. The | ||||||||||||||||||
| 901 | # elements are copied, so that a caller mutating their array partway | ||||||||||||||||||
| 902 | # through a run cannot move the cursor out from under it. | ||||||||||||||||||
| 903 | 3 | my @queue = @$source; | |||||||||||||||||
| 904 | 3 | my $i = 0; | |||||||||||||||||
| 905 | 3 | ▶ | $self->set_input_cb(sub { $i < @queue ? $queue[ $i++ ] : () }); | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||
| 906 | } | ||||||||||||||||||
| 907 | else { | ||||||||||||||||||
| 908 | 1 | croak 'inputs must be a code reference or an array reference'; | |||||||||||||||||
| 909 | } | ||||||||||||||||||
| 910 | |||||||||||||||||||
| 911 | 7 | return $self; | |||||||||||||||||
| 912 | } | ||||||||||||||||||
| 913 | |||||||||||||||||||
| 914 | sub library_paths { | ||||||||||||||||||
| 915 | 3 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
library_paths: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||
| 916 | |||||||||||||||||||
| 917 | 3 | ▶ | if (@_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 918 | 1 | my $paths = shift; | |||||||||||||||||
| 919 | 1 | ▶ | $paths = [] unless defined $paths; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 920 | 1 | ▶ | ref $paths eq 'ARRAY' | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 921 | or croak 'library_paths must be an array reference'; | ||||||||||||||||||
| 922 | 1 | $self->set_attr('JQ_LIBRARY_PATH', [@$paths]); | |||||||||||||||||
| 923 | 1 | return $self; | |||||||||||||||||
| 924 | } | ||||||||||||||||||
| 925 | |||||||||||||||||||
| 926 | 2 | my $paths = $self->attr('JQ_LIBRARY_PATH'); | |||||||||||||||||
| 927 | 2 | ▶ | return defined $paths ? $paths : []; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 928 | } | ||||||||||||||||||
| 929 | |||||||||||||||||||
| 930 | # Pass $_[0] through unaliased: copying it (my $data = ...) would strip | ||||||||||||||||||
| 931 | # the identity of Perl's native boolean SVs on perls before 5.36. | ||||||||||||||||||
| 932 | sub process { | ||||||||||||||||||
| 933 | 76 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 934 | 76 | my $results = _xs_process($self, $_[0], 0, -1, 0); | |||||||||||||||||
| 935 | 69 | ▶ | return wantarray ? @$results : $results; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 936 | } | ||||||||||||||||||
| 937 | |||||||||||||||||||
| 938 | sub process_json { | ||||||||||||||||||
| 939 | 18 | ▶ | my $self = shift; | ||||||||||||||||
|
Subroutine
process_json: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||
| 940 | 18 | my $text = shift; | |||||||||||||||||
| 941 | |||||||||||||||||||
| 942 | # -1 tells the XS side to use the object's own output options; anything | ||||||||||||||||||
| 943 | # given here is merged over them for this call only. | ||||||||||||||||||
| 944 | 18 | my ($flags, $raw) = (-1, 0); | |||||||||||||||||
| 945 | 18 | ▶ | if (@_) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 946 | 1 | my %opts = (%{ $self->output_options }, @_); | |||||||||||||||||
| 947 | 1 | ($flags, $raw) = _output_flags(\%opts); | |||||||||||||||||
| 948 | } | ||||||||||||||||||
| 949 | |||||||||||||||||||
| 950 | 18 | my $results = _xs_process($self, $text, 1, $flags, $raw); | |||||||||||||||||
| 951 | 17 | ▶ | return wantarray ? @$results : $results; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 952 | } | ||||||||||||||||||
| 953 | |||||||||||||||||||
| 954 | sub to_json { | ||||||||||||||||||
| 955 | 4 | ▶ | my $data = shift; | ||||||||||||||||
|
Subroutine
to_json: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 956 | 4 | my %opts = @_; | |||||||||||||||||
| 957 | 4 | ▶ | croak 'to_json does not take a raw option' if $opts{raw}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 958 | 3 | my ($flags) = _output_flags(\%opts); | |||||||||||||||||
| 959 | 3 | return _to_json($data, $flags); | |||||||||||||||||
| 960 | } | ||||||||||||||||||
| 961 | |||||||||||||||||||
| 962 | # Don't clone the underlying C struct into new ithreads. | ||||||||||||||||||
| 963 | 0 | ▶ | sub CLONE_SKIP { 1 } | ||||||||||||||||
|
Subroutine
CLONE_SKIP: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 964 | |||||||||||||||||||
| 965 | 6 | 1; | |||||||||||||||||