dxma / perl5-json-jq

JSON::JQ - jq (https://stedolan.github.io/jq/) library binding
MIT License
5 stars 5 forks source link

Wrap `use FindBin` et al in an "eval" #15

Open DabeDotCom opened 1 year ago

DabeDotCom commented 1 year ago

I know it might seem a little pathological, but FindBin can die if its script is coming via named pipe:

  _PERL='use Data::Dumper; print Dumper( JSON::JQ->new({ script => "." })->process({json => <>}) )'
  echo '{"one": 1, "two": 2}' | perl -MJSON::JQ -0777 <(echo "$_PERL")

  Cannot find current script '/dev/fd/63' at /System/Library/Perl/5.30/FindBin.pm line 166.
  BEGIN failed--compilation aborted at /System/Library/Perl/5.30/FindBin.pm line 166.

(I use this technique to generate much more complex query scripts on the fly, for example...)

dxma commented 1 year ago

hi,

Since you've saved the script in a shell variable already, call the 2nd line in the way below. FindBin will figure it out this time: $ echo '{"one": 1, "two": 2}' | perl -MJSON::JQ -0777 -e "$_PERL" $VAR1 = { 'one' => 1, 'two' => 2 };

DabeDotCom commented 1 year ago

Since you've saved the script in a shell variable already, call the 2nd line in the way below. FindBin will figure it out this time: $ echo '{"one": 1, "two": 2}' | perl -MJSON::JQ -0777 -e "$_PERL" $VAR1 = { 'one' => 1, 'two' => 2 };

I'm trying to remember why -e/-E didn't Do What I Mean in the first place... I'm sure I wouldn't have put myself through such /dev/fd/### shenanigans if it weren't strictly necessary.

I do remember running into problems under older versions of Bash 3.x (macOS) trying to run something like:

#!/usr/bin/env bash
perl -E "$(cat <<'END_OF_PERL`
  print "HERE!\n";
END_OF_PERL
)"

But $(...) is definitely more portable than <(...) — and like the song says: "We don't talk about `backticks`, no no no!" 🎶

(Also, I can't seem to reproduce any of those problems at the moment, even as far back as MacOS 10.6 Snow Leopard! «scratching head»)

Hmm, was it a command-line limit in bash? Then I might have considered (export _PERL; perl -E 'eval $ENV{_PERL}')... Or did I want to hide/minimize the script contents from ps and/or ps -e? No, <(echo "$_PERL") suffers the same fate... And both -E and <(...) seem to handle #line 69-type directives just fine, which was another thing I thought it might have been.

I'm wondering if maybe it had something to do with passing things into docker? Doubtful: named pipes are even LESS resilient across containers... Quoting issues with bash -c, perhaps?

Ida Know

Regardless, it's an idiom that I've managed to work into a bunch of utility wrappers and whatnot over the years; this is the first time I've run into a CPAN module that couldn't handle it. ¯\_(ツ)_/¯

PS: I suppose a "more correct" solution might be for me to ask the maintainers of FindBin (aka Perl/perl5) if they'll accept a PR to handle -p $0 the same as -e and - — i.e., return the current working directory — but I don't know whether there are any esoteric Windows/VMS/AIX gotchas I haven't considered... 🤔

Anyway, thanks for helping me move the ball forward, even if it meant dusting off some really old brain cells! «grin»