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

JSON API minimal examples

The JSON API standard is quite large, here are some basic examples. These show you do not need to implement many fields to adhere to JSON API.

Success response: (the data array is optional, it could be ommitted if the response is always a single object).
Failure response:

Software logging philosophy

Ideas:

  • Always set up logging from the start of the application, even if there is nothing to log.
    • Imagine in future there is an error reported in the front-end.
    • To investigate, there could be more error information in the logs.
    • And even if not, to investigate further, more debug info could be added to the log temporarily, even for a specific client.
    • If that requires you to set up logging infrastructure, it's much less likely to be done at all
    • So it's important to have a logging system in place for that reason.
  • Distinguish types of logging (see other post):
    • Event - this can include debug, info, warn, error. It is logged and that's it.
    • Exception - unexpected error that requires action to resolve. A human should be notified.
    • Request - every request made (higher/framework level)
  • ...

How to defeat Sub::Quote and Sub::Defer

Do you use the Perl debugger?

Are you frustrated when you hit a maze of Sub::Quote twisty passages, all alike?

Then this guide is for you!

Walkthrough solution for Sub::Quote:

  • n (north)
  • n (north)
  • s (south)
  • # Boom! You should now be on the other side of the maze.
Walkthrough solution for Sub::Defer:
  • to do
(source: Text adventures of the eighties and nineties, and Perl coding adventures of the naughties)

Linux renaming examples, when filenames have spaces

Do your filenames have spaces? Do you curse bash for making it difficult to loop over them?

Fear not! (and wash that potty mouth out with soap). This loop is all you need:

find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do ls "$file"; done

# Rename "foo.mp3.mp3" to "foo.mp3"

find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do file=`basename "$file"`; newfile=$(perl -le'$ARGV[0]=~s/\.mp3//;print$ARGV[0]' "$file"); echo mv "$file" "$newfile"; done


# Add a leading zero to numbered filenames

find . -type f -name "064*" -print0 | while IFS= read -r -d '' file; do file=`basename "$file"`; echo mv "$file" "0$file"; done

# The above commands will print out what they're about to do. Check it is what you expect! Then make this change to run them for real:

Change: echo mv foo bar
To: `mv foo bar`

# ^ Those ` are backticks

# Note the order of files is not sorted


Web scraping for fun

I wanted to know the relative popularity of different locations in which Hindi movies are filmed.

I achieved this in about 15 minutes, with as little coding as I could manage.

Resources used: Linux, bash, wget, grep, uniq, sort, Chrome, XPath helper extension, a text editor, regexes.