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

Determine a day's ordinal suffix: 1st, 2nd, 3rd, 4th, etc.

e.g. for dates you don't want to display "19 July" but rather "19th July":

my $ordinal;
if ($foo =~ /(?<!1)1$/) {
    $ordinal = 'st';
} elsif ($foo =~ /(?<!1)2$/) {
    $ordinal = 'nd';
} elsif ($foo =~ /(?<!1)3$/) {
    $ordinal = 'rd';
} else {
    $ordinal = 'th';
}

Bonus points if you can integrate this with DateTime's strftime() method neatly.

(source)

Lock rows with DBIx::Class

$schema->txn_do(sub{

    $foos_rs->search({}, {for => 'update'})->all; # Lock rows

    # Check status of something

    # Update it
});

Code works on one environment but not another

What can differ between environments? Check the following:

* Your code (obviously)
* Versions of other dependent packages - both in-house and third-party
* Versions of other installed in-house modules
* Versions of other Perl CPAN modules
* Processes which didn't die when you restarted the app
* Data in the database
* Number of rows in tables, i.e. is some limit being hit?
* Schema of the database
* Browser cache
* Server side web cache
* Files on disk, e.g. cached print documents

(source: a decade's experience building software)

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;