Using App::p Command Line Magic to Parse Vimperator History
I noticed today that my firefox has been really slow lately. I use a firefox plugin named vimperator which provides vim-like keybindings for firefox. I have it configured it to store 5000 command history entries, which is considerably more than the default (500). It has always been in the back of my mind that this might come back to bite me one day. So today I decided to check how many entries have so far been saved in the history file. I easily found that the history is stored in a json file located at ~/.vimperator/info/default/history-command. Running wc (word count) wouldn't help because all the data is stored on one line. I noticed that the file simply contains a serialized json array. To determine the size of this array, I used App::p and whipped up this simple one-liner:
p 'p @{jl r "history-command"} + 0'
And tada, I have 3997 history entries.
To understand what this does you can just run p
or man p
or man App::p
for documentation:
Usage: p [-lneE etc] 'code'
The code can make use of:
r to File::Slurp::read_file
w to File::Slurp::write_file
S to say()
p to print()
dd to Data::Dump::dd()
jd to JSON::XS::encode (utf8/pretty)
jl to JSON::XS::decode (utf8/allow nonref) a thing
yd to YAML::Dump()
yl to YAML::Load()
Basically the above one-liner does the equivalent of:
perl -MJSON::XS -MFile::Slurp -E \
'print @{decode_json(read_file("history-command"))} + 0'
By the way, the "+ 0" evaluates the array in scalar context which returns the size of the array. So did I find out if this was the cause of my firefox slowdown? No, it was just a misbehaving tab. But at least I had fun playing with App::p.
There is another neat thing about App::p.
Because it happens to provide an executable named 'p', you can install it
simply with sudo cpanm p
.
Leave a comment