A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Split one long line into shorter lines

Command:

fold file_with_long_lines.txt

...prints to STDOUT

Working with PDFs on Linux

Combine several PDFs into one:

pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf

Convert image to PDF:

convert page1.png page2.png mydoc.pdf

Convert PDF to image:

convert foo.pdf foo.png

(source, source, source)

Reduce size of a PDF by converting to high quality images reducing size and rebuilding PDF:

convert -density 150 foo.pdf -quality 100 foo.png
for k in 0 1 2 3 4; do convert "foo-"$k".jpg" -resize '75%' "foo_smaller"$k".jpg"; done
convert foo_smaller* foo_smaller.pdf

(source, source, source)

Make all the pages of the PDF the same size:

mogrify -resize x1000 *.jpg
for i in *.jpg; do convert -density x1000 -units PixelsPerInch ${i} ${i}; done
convert *.jpg result.pdf


More tricks:

convert -background white -page a4 -density 150 -quality 80 -compress jpeg *.jpg result.pdf

Perl code review

What is wrong with this line of code?

return !grep { $_ == $item->id } grep { $_ } @$scanned_items;

A lot of things are implicit in it.
It returns 1 (true) if the count is zero, and "" (emtpy string - false) if the count is non-zero.

I would prefer for it to be written more explicitly:

my $item_count = scalar grep { defined $_ && $_ == $item->id } @$scanned_items;
return ($item_count > 0) ? 0 : 1;

A unary plus sign before curly brackets in Perl

Curly braces are overloaded - they can signify either a hash or a block

+{ Prepending a plus sign forces it to be interpreted as a hash.

{; Adding a semicolon forces it to be interpreted as a block.

Sources:
- The Case of the Overloaded Curlys
- perlref

Easily save music playlists from Youtube on Ubuntu

git clone https://github.com/rg3/youtube-dl.git
cd youtube-dl
./youtube-dl 'https://www.youtube.com/playlist?list=PL517964F9D64C8D0B' --ignore-errors --extract-audio --audio-quality 0 --audio-format mp3 --prefer-ffmpeg --output '%(playlist_index)s - %(title)s.%(ext)s'

(source, source)

...but then the mp3 files have the wrong length and can't be fast forwarded properly in many players (youtube-dl 2014.04.30.1)

To fix them:

sudo apt-get install vbrfix
cd /path/to/mp3s
find . -type f -iname '*.mp3' -exec vbrfix {} {} \;
rm vbrfix.log vbrfix.tmp

(source, source)