How to Run the Programming Hacks

The few programmatic hacks in this book run on the command line (that's the Terminal for Mac OS X folks, and the DOS command window for Windows users). Running a hack on the command line invariably involves the following steps:

  1. Type the program into a garden-variety text editor: Notepad on Windows, TextEdit on Mac OS X, vi or Emacs on Unix/Linux, or anything else of the sort. Save the file as directed—usually as scriptname .pl (the pl bit stands for Perl, the predominant programming language used in Mind Performance Hacks).

  2. Alternately, you can download the code for all of the hacks online at http://www.oreilly.com/catalog/mindperfhks. There you'll find a zip archive filled with individual scripts already saved as text files.

  3. Get to the command line on your computer or remote server. In Mac OS X, launch the Terminal (Applications→Utilities→Terminal). In Windows, click the Start button, select Run..., type command, and hit the Enter/Return key on your keyboard. In Unix...well, we'll just assume you know how to get to the command line.

  4. Navigate to where you saved the script at hand. This varies from operating system to operating system, but usually involves something like cd ~/Desktop (that's your Desktop on the Mac).

  5. Invoke the script by running the programming language's interpreter (e.g., Perl) and feeding it the script (e.g., scriptname .pl), like so:

  6. $ perl scriptname.pl
  7. Most often, you'll also need to pass along some parameters—your search query, the number of results you'd like, and so forth. Simply drop them in after the script name, enclosing them in quotes if they're more than one word or if they include an odd character or three:

  8. $ perl scriptname.pl '"much ado about nothing" script' 10
  9. The results of your script are almost always sent straight back to the command-line window in which you're working, like so:

  10. $ perl scriptname.pl '"much ado about nothing" script' 10
                      
         1. "Amazon.com: Books: Much Ado About Nothing: Screenplay ..." 
         [http://www.amazon.com/exec/obidos/tg/detail/-/0393311112?v=glance]
         2. "Much Ado About Nothing Script" 
         [http://www.signal42.com/much_ado_about_nothing_script.asp]
         ...

Tip

The ellipsis points (...) signify that we've cut off the output for brevity's sake.

  1. To prevent the output from scrolling off your screen faster than you can read it, on most systems you can pipe (redirect) the output to a little program called more:

  2. $ perl scriptname.pl | more
  3. Hit ^ and the Enter/Return key on your keyboard to scroll through line by line, the spacebar to leap through page by page.

  4. You'll also sometimes want to direct output to a file for safekeeping, importing into your spreadsheet application, or displaying on your web site. This is as easy as:

  5. $ perl scriptname.pl > output_filename.txt
  6. And to pour some input into your script from a file, simply do the opposite:

  7. $ perl scriptname.pl < input_filename.txt

Don't worry if you can't remember all of this; each programmatic hack has a "Running the Hack" section that shows you just how it's done.

Tip

Fancy trying your hand at a spot of programming? O'Reilly's best-selling Learning Perl (http://www.oreilly.com/catalog/learnperl4) by Randal L. Schwartz, Tom Phoenix, and brian d. foy provides a good start.

Get Mind Performance Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.