Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this Perl one-liner actually work?

Tags:

So, I happened to notice that last.fm is hiring in my area, and since I've known a few people who worked there, I though of applying.

But I thought I'd better take a look at the current staff first.

Everyone on that page has a cute/clever/dumb strapline, like "Is life not a thousand times too short for us to bore ourselves?". In fact, it was quite amusing, until I got to this:

perl -e'print+pack+q,c*,,map$.+=$_,74,43,-2,1,-84, 65,13,1,5,-12,-3, 13,-82,44,21, 18,1,-70,56, 7,-77,72,-7,2, 8,-6,13,-70,-34' 

Which I couldn't resist pasting into my terminal (kind of a stupid thing to do, maybe), but it printed:

Just another Last.fm hacker,

I thought it would be relatively easy to figure out how that Perl one-liner works. But I couldn't really make sense of the documentation, and I don't know Perl, so I wasn't even sure I was reading the relevant documentation.

So I tried modifying the numbers, which got me nowhere. So I decided it was genuinely interesting and worth figuring out.

So, 'how does it work' being a bit vague, my question is mainly,

What are those numbers? Why are there negative numbers and positive numbers, and does the negativity or positivity matter?

What does the combination of operators +=$_ do?

What's pack+q,c*,, doing?

like image 242
magnetar Avatar asked Jun 01 '12 22:06

magnetar


People also ask

How do I print something in Perl?

print() operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) is used as a delimiter to this operator.

How do I use argv in Perl?

The @ARGV array holds the command line argument. There is no need to use variables even if you use "use strict". By default, this variable always exists and values from the command line are automatically placed inside this variable. To access your script's command-line arguments, you just need to read from @ARGV array.

What is Perl PE?

The perl -pe command. 1. Unix command to replace old date with current date dynamically.


2 Answers

This is a variant on “Just another Perl hacker”, a Perl meme. As JAPHs go, this one is relatively tame.

The first thing you need to do is figure out how to parse the perl program. It lacks parentheses around function calls and uses the + and quote-like operators in interesting ways. The original program is this:

print+pack+q,c*,,map$.+=$_,74,43,-2,1,-84, 65,13,1,5,-12,-3, 13,-82,44,21, 18,1,-70,56, 7,-77,72,-7,2, 8,-6,13,-70,-34 

pack is a function, whereas print and map are list operators. Either way, a function or non-nullary operator name immediately followed by a plus sign can't be using + as a binary operator, so both + signs at the beginning are unary operators. This oddity is described in the manual.

If we add parentheses, use the block syntax for map, and add a bit of whitespace, we get:

print(+pack(+q,c*,,             map{$.+=$_} (74,43,-2,1,-84, 65,13,1,5,-12,-3, 13,-82,44,21,                          18,1,-70,56, 7,-77,72,-7,2, 8,-6,13,-70,-34))) 

The next tricky bit is that q here is the q quote-like operator. It's more commonly written with single quotes:

print(+pack(+'c*',             map{$.+=$_} (74,43,-2,1,-84, 65,13,1,5,-12,-3, 13,-82,44,21,                          18,1,-70,56, 7,-77,72,-7,2, 8,-6,13,-70,-34))) 

Remember that the unary plus is a no-op (apart from forcing a scalar context), so things should now be looking more familiar. This is a call to the pack function, with a format of c*, meaning “any number of characters, specified by their number in the current character set”. An alternate way to write this is

print(join("", map {chr($.+=$_)} (74, …, -34))) 

The map function applies the supplied block to the elements of the argument list in order. For each element, $_ is set to the element value, and the result of the map call is the list of values returned by executing the block on the successive elements. A longer way to write this program would be

@list_accumulator = (); for $n in (74, …, -34) {     $. += $n;     push @list_accumulator, chr($.) } print(join("", @list_accumulator)) 

The $. variable contains a running total of the numbers. The numbers are chosen so that the running total is the ASCII codes of the characters the author wants to print: 74=J, 74+43=117=u, 74+43-2=115=s, etc. They are negative or positive depending on whether each character is before or after the previous one in ASCII order.

For your next task, explain this JAPH (produced by EyesDrop).

''=~('(?{'.('-)@.)@_*([]@!@/)(@)@-@),@(@@+@)' ^'][)@]`}`]()`@.@]@%[`}%[@`@!#@%[').',"})') 

Don't use any of this in production code.

like image 74
Gilles 'SO- stop being evil' Avatar answered Oct 17 '22 09:10

Gilles 'SO- stop being evil'


The basic idea behind this is quite simple. You have an array containing the ASCII values of the characters. To make things a little bit more complicated you don't use absolute values, but relative ones except for the first one. So the idea is to add the specific value to the previous one, for example:

  1. 74 -> J
  2. 74 + 43 -> u
  3. 74 + 42 + (-2 ) -> s

Even though $. is a special variable in Perl it does not mean anything special in this case. It is just used to save the previous value and add the current element:

map($.+=$_, ARRAY) 

Basically it means add the current list element ($_) to the variable $.. This will return a new array with the correct ASCII values for the new sentence.

The q function in Perl is used for single quoted, literal strings. E.g. you can use something like

q/Literal $1 String/ q!Another literal String! q,Third literal string, 

This means that pack+q,c*,, is basically pack 'c*', ARRAY. The c* modifier in pack interprets the value as characters. For example, it will use the value and interpret it as a character.

It basically boils down to this:

#!/usr/bin/perl use strict; use warnings;  my $prev_value = 0;  my @relative = (74,43,-2,1,-84, 65,13,1,5,-12,-3, 13,-82,44,21, 18,1,-70,56, 7,-77,72,-7,2, 8,-6,13,-70,-34); my @absolute = map($prev_value += $_, @relative);  print pack("c*", @absolute); 
like image 35
Ulrich Dangel Avatar answered Oct 17 '22 09:10

Ulrich Dangel