Better Living Through Thinking

Finding Palindromes

Thu, 01 Mar 2007

A few years ago, a co-worked challenged me to write a program to find palindromes. I'd forgotten about it until I just came across the code, which I present here now (named "is_palindrome"):

#!/usr/local/bin/perl -w
use strict;

## Scott Wiersdorf
## Created: Wed Apr 23 13:06:55 MDT 2003
...

(read more...)

[ category: /perl | link: finding_palindromes ]

Truncating a Sentence

Thu, 15 Feb 2007

Your problem: find the last space in a line before a given number of characters and split the sentence on that space (so as not to break a word in half).

One solution:

my $string = "This is a sentence with a variety of spaces in it from which one could choose\n";
print substr($string, 0, rindex($string, ' ', 30));

The trick is to use *rindex*, which works like this: "Start at position 30 in the string (which occurs in the middle of the word "variety") and look toward the start of the string; find the first occurance of ' ' and return its string index".

[ category: /perl | link: truncating_sentences ]

Using Perl's Ternary Operator as a Switch/Case Statement

Wed, 18 Oct 2006

I'm certain someone has already come up with this, but I was recently refactoring some old code that looked like this:

my $code = get_error_code();
my $why;
if ($code == 1) {
  $why = "Invalid characters";
} elsif ($code == 2) {
  $why = "Unknown Domain";
  ...

(read more...)

[ category: /perl | link: ternary_switch ]

Parsing HTML tables with HTML::Parser

Mon, 09 Oct 2006

We can parse HTML tables into native Perl data structures (arrays, etc.) with HTML::Parser. Here is one way (maybe not the most HTML::Parser-ish way, but it works); we use a simple state mechanism to remember if we're in a table, row, or table cell.

If we're in a cell, we save the cell data found there to a variable ($cell). Once we reach the end of a cell (a </td> tag), we append that cell to @row. Each row is in turn appended to the @table array as a reference. ...

(read more...)

[ category: /perl | link: html_parser_table_parsing ]

Dumping HTML tables using HTML::Parser

Thu, 05 Oct 2006

HTML::Parser is a powerful and mysterious module, which I have to re-learn every year or so when I have to do something with HTML. Here's a little program to extract and print all tables in an HTML document, including nested tables:

#!/usr/bin/perl
use strict;
use warnings;
use HTML::Parser;
...

(read more...)

[ category: /perl | link: html_parser_tables ]

Lightweight Tracing of a Perl Program

Thu, 05 Oct 2006

Perl filters are truly a marvel. The following simple filter allows you to trace your programs without changing the program to be traced other than adding:

use Tracer;

to the top of the module or program to be traced. If you don't mind wholesale tracing, you can just run your program *completely unmodified* like this: ...

(read more...)

[ category: /perl | link: lightweight_tracing ]

Subroutine Tracing

Thu, 05 Oct 2006

Sometimes we want to see the call chain of a subroutine, just to see where our data has been. For example, a program:

blech();
exit;

might have this kind of chain:

blech() -> baz() -> bar() -> foo()
...

(read more...)

[ category: /perl | link: subroutine_tracing ]

Yet another download script

Sat, 26 Aug 2006

Here is a very simple download script; it has three configurable variables near the top of the file (self-explanitory).

The program (serve.cgi) serves files out of a specified directory; this directory should not otherwise be accessible by the web server (e.g., under a document root somewhere).

Access to the script should be controlled by some kind of web server-based authentication and authorization (e.g., basic auth or ...

(read more...)

[ category: /perl | link: download_script ]

Printing the last 'n' characters of each line in a file

Fri, 25 Aug 2006

We all know how to print the first 12 characters of a file using, say, the 'cut' utility in Unix:

cut -c 1-12 input.txt

But how about the *last* 12 characters of each line?

We can use a double-'rev' pipeline:

...

(read more...)

[ category: /perl/one-liners | link: last_n_characters ]

Creating Devices

Wed, 22 Feb 2006

I'm in a situation where I need to duplicate some devices elsewhere. Devices are special files that can't simply be copied--they have to be created using 'mknod'.

First we stat our device (e.g., /dev/urandom):

my $file = '/dev/urandom';
my @stat = lstat($file);

...

(read more...)

[ category: /perl | link: create_devices ]

Audio Broadcast

(standby)

Moon Status

Phase: 99.97%
Illuminated: 0.00%
Age (days): 29.52
moon phase 0.999671847443174 Sun May 20 17:33:32 MDT 2012