1
\$\begingroup\$

Task

Make code given on the command line that takes an unpacked hex value as input and evals that in Perl. Like this:

perl -e 'map{eval}pack q/h*/,q/072796e6470222458656275672370202d6f62756020222b302072796e6470222478616e60202f6e6560202771697020247f6020246f602029647e2c5e622/'

But with one additional restriction: Your code must survive any level of ssh

(and the example above does not survive because the space would need to be quoted:

# This does not work
ssh server perl -e 'map{eval}pack q/h*/,q/072796e6470222458656275672370202d6f62756020222b302072796e6470222478616e60202f6e6560202771697020247f6020246f602029647e2c5e622/'
# This would survive 1 level of ssh but not 2
ssh server perl -e 'map{eval}pack\ q/h*/,q/072796e6470222458656275672370202d6f62756020222b302072796e6470222478616e60202f6e6560202771697020247f6020246f602029647e2c5e622/'

)

So:

ssh server your_code hexvalue
ssh server ssh server2 your_code hexvalue
ssh server ssh server2 ssh server3 your_code hexvalue

should all give the same result. You will not be told the number of levels of ssh.

You can assume the login shell is bash.

You can choose to include the hex value in the program (in which case it obviously does not count towards the length of your code) or have it given as an argument to your program.

You are also allowed to preprocess the hex value (e.g. convert it to another format) and use that value instead. But you cannot do the eval locally: It must be run on the remote server. The preprocessing code is of course also code thus also part of the byte count.

Test

Given the value:

072796e6470222458656275672370202d6f62756020222b302072796e6470222478616e60202f6e6560202771697020247f6020246f602029647e2c5e622

your program should print:

There's  more  than  one  way  to  do  it.

And:

4216d3223616d656c622b302072796e6470222140242160296e6024786560246563737562747e2c5e622

should print:

A camel in the dessert.

Finally:

072796e647020686f63747e616d65606

should print the hostname of the server that you ssh to.

Scoring

Code that only uses Perl wins over code that uses Bash+Perl which wins over code in any other languages.

In case of a tie: Code without warnings wins.

In case of a tie: Shortest code wins.

Edit

Clarified preprocessing is allowed, but not for free, and warnings are frowned upon, but not illegal.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Given the rule that allows you to preprocess the value before inserting it into the program, how is such a program to be scored? If the preprocessed value isn’t included in the byte count, nothing would stop you from asserting that the entire program is part of the preprocessed value (for example, you might assert that the eval+v in ais523’s answer is just as much a part of the preprocessed format as the .s), leading to a score of 0 bytes. \$\endgroup\$ Commented Jan 22, 2022 at 22:43
  • \$\begingroup\$ @AndersKaseorg Clarified that preprocessing is not for free. \$\endgroup\$
    – Ole Tange
    Commented Jan 23, 2022 at 1:50

2 Answers 2

4
\$\begingroup\$

Perl only; (+0 bytes) 23 bytes

Saved two bytes thanks to Ole Tange themself! ('+' -> +)

map{eval}pack+q/h*/,q/$hexvalue/

Disclaimer: I have no experience with Perl whatsoever.
However, this seems to work fine, I simply replaced the single space in the example with '+', which has now been golfed to just +.

For example:

ssh localhost ssh localhost [...] ssh localhost perl -e 'map{eval}pack'+'q/h*/,q/072796e647020686f63747e616d65606/'

(We can't seem to ssh even to localhost over on TIO though)

\$\endgroup\$
9
  • \$\begingroup\$ @OleTange - does this conform to your specification? I notice that your examples are ssh server ssh server2 ... ssh serverN your_code hexvalue which this one does not fit with (i.e. the hex value is in the code much like your first example). \$\endgroup\$ Commented Jan 21, 2022 at 23:26
  • 1
    \$\begingroup\$ Yes, that is OK: "You can choose to include the hex value in the program". \$\endgroup\$
    – Ole Tange
    Commented Jan 21, 2022 at 23:32
  • \$\begingroup\$ "no experience with Perl whatsoever" - seems like you know more than the basics. I cant even understand what your code means but okey 1+ for the bytes \$\endgroup\$
    – DialFrost
    Commented Jan 21, 2022 at 23:40
  • 2
    \$\begingroup\$ @DialFrost Nope, I've never used Perl before; I really just tried it, thinking it might work :) \$\endgroup\$ Commented Jan 21, 2022 at 23:42
  • 1
    \$\begingroup\$ map{eval} can just be eval+ \$\endgroup\$
    – Sisyphus
    Commented Jan 22, 2022 at 4:06
0
\$\begingroup\$

Pure Perl 5, 6 bytes + 8 bytes for a change to the preprocessing format = 14 bytes

eval+v

Try it online!

This is followed by the code that you want to evaluate written with codepoints in decimal and separated by periods, e.g.

perl -e eval+v112.114.105.110.116.32.50.42.50

Produces a warning about ambiguous syntax (the + could be interpreted as either unary + (a no-op) or as addition), but the parse that Perl chooses by default works.

Encoders

The format listed in the question is not the usual hex encoding; the first example starts with 07, which in a normal hexdump, would represent the BEL character. I suspect it has been produced using code that's something like this:

print unpack("h*", "print 2*2")

(A "regular" hexdump would use the H* unpack format, rather than h*.)

To produce the format used by this answer, you unpack with c* rather than h*, and with $, set to ".":

$,=".";print unpack("C*", "print 2*2")

Encoding it using Perl therefore takes 8 more bytes than encoding the format stated in the question.

Alternatively, you can use this online encoder, written in Jelly.

Explanation

eval+vn.n.n.n.n.n…
eval                 Evaluates a string literal as code
    +                No-op (used here as a whitespace substitute)
     vn.n.n.n.n.n…   String literal specified using character codes

All the characters in this program are invariant under shell escaping (it's just letters, digits, + and .), so it can be passed through any number of shell command lines and will remain intact.

\$\endgroup\$
7
  • \$\begingroup\$ I don’t see any justification in the question for subtracting the byte count of the suspected code for encoding the format stated in the question. \$\endgroup\$ Commented Jan 23, 2022 at 2:29
  • \$\begingroup\$ The question uses a nonstandard format (and hasn't documented it) – thus, preprocessing it into any other format involves undoing the nonstandard format first to get back to the original string. In practice, you're unlikely to generate strings in that format except via using Perl unpack, and so if you wanted to solve this sort of problem practically, you'd modify your existing encoder rather than using the existing encoder, reversing the encoding to get back at the original string, and redoing the encoding with slightly different settings. \$\endgroup\$
    – ais523
    Commented Jan 23, 2022 at 2:34
  • 2
    \$\begingroup\$ Then ask the question author whether the scoring should be changed. You can’t just make up your own scoring. \$\endgroup\$ Commented Jan 23, 2022 at 2:36
  • 2
    \$\begingroup\$ A different question might have made more sense. It would, however, be a different question. You need to ask the question author if they want to change it. \$\endgroup\$ Commented Jan 23, 2022 at 2:45
  • 1
    \$\begingroup\$ It looks like you’ve been marking all your answers as community wiki; that’s why you aren’t gaining any rep. \$\endgroup\$ Commented Jan 23, 2022 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.