Better Living Through Thinking |
|
Finding PalindromesThu, 01 Mar 2007A 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 ... Truncating a SentenceThu, 15 Feb 2007Your 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". Using Perl's Ternary Operator as a Switch/Case StatementWed, 18 Oct 2006I'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";
...
Parsing HTML tables with HTML::ParserMon, 09 Oct 2006We 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
( Dumping HTML tables using HTML::ParserThu, 05 Oct 2006HTML::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; ... Lightweight Tracing of a Perl ProgramThu, 05 Oct 2006Perl 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: ... Subroutine TracingThu, 05 Oct 2006Sometimes 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() ... Yet another download scriptSat, 26 Aug 2006Here 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 ... Printing the last 'n' characters of each line in a fileFri, 25 Aug 2006We 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: ... Creating DevicesWed, 22 Feb 2006I'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); ... |
Audio Broadcast(standby)Moon StatusPhase: 33.17%Illuminated: 74.54% Age (days): 9.79
Mon May 20 09:56:28 MDT 2013 |