Learning Perl Challenge: popular history

For June’s challenge, write a program to read the history of shell commands and determine which ones you use the most.

In bash, you can set the HISTFILE and HISTFILESIZE to control history‘s behavior. To get the commands to count, you can read the right file or shell out to the history command. From there, you have to decide how to split up each line to figure out what the command is. Can you handle commands with relative paths and absolute paths so you don’t count them separately?

This was an meme for a little while, and I wish I could find my answer to it. If you find my answer, please let me know.

At first I set a few variables which can be altered on the command line. Unfortunately I’m not familiar with Pod::Usage yet…
The history file could be named differently or be at a different location. The format of the file could also vary. The default is to assume that the file consists of just lines directly beginning with the command. I’ve seen history files where there is a number first. In such a case giving a position of the command might come in handy.
By default the program displays the 10 most popular commands, but the number can also be changed…

First the history file is parsed line by line creating a hash with each basename of the command as a key with invocation count as a value.

After the whole file has been read and the history hash is complete another hash is created. This time the count is the key and the value is an array of commands.

Before displaying the most popular commands, the amount of keys is checked so that not more elements are sliced than actually existant.