perl -pi -e 's/replace this/join "", map { ("a" .. "z", "A" .. "Z", 0 .. 9)[rand(62)] } 1 .. 64/eg' example.php
Let's break this down into its pieces.
("a" .. "z", "A" .. "Z", 0 .. 9)
is a list that contains the characters you want to be in the random string.
[rand(62)]
This is indexing the list above at a random location (using the rand
function). The 62 corresponds to the number of items in the list. The rand function returns a number between zero and the number you gave it minus one. Happily, arrays and lists are indexed starting at zero in Perl 5, so this works out perfectly. So, every time that piece of code is run, you will get one random character from the list of acceptable characters.
The map
takes a code block and a list as arguments. It runs the code block and returns the result for every item in the list handed to it. The list is 1 .. 64
, so the code block will run sixty-four times. Since the code block contains the code that generates a random character, the result of the map
function is sixty-four random characters.
The join
function takes a delimiter and a list and returns the list as a string delimited by the delimiter (e.g. join ",", "a", "b", "c"
returns "a,b,c"
). In this case we are using an empty string as the delimiter, so it just produces a string made up of the characters in the list (i.e. the sixty-four random characters).
Now we are ready to look at the substitution. It looks for every instance (because of the /g
option) of the string "replace this"
and runs the code in the replacement side (because of the /e
options) and replaces the string "replace this"
with the value of the last statement executed in the replacement side (in this case, the return value of join
).