Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

Hmmm, July 4, and I'm fooling around on a computer. It seems that I'm often happiest when I'm working on something, at least something fun. Today's travels bring me to the Jiki, or JikiJikiJava, a Java replacement for the Wiki. Actually, it may go further than the Wiki, in that it is a "full-blown distributed component-based server".

I'm not recommending this product yet, because I haven't done anything but look at the web page, but I don't want to lose the URL. So here is the link to JiveLint, a free product that promises to "find unused code and variables" in Java programs, and much more (potential bugs, weak points, code conventions). Some dev teams are great and have no dead code in their projects. Others, well ... a tool like this might be very helpful. :) Borland's OptimizeIt is the only other product I've seen for far to make this claim.

In honor of unofficial socket day, here's a Sun tut on reading and writing from/to a socket. Of course if you don't know what a socket is, you may want to start with what is a socket? instead.

Nothing like a long motorcycle ride (during which I experienced the first suv !#@%$# trying to ride in my lane; I now know where the horn is on the bike), followed by finding the ten gates, which in turn helped me find the primary point online.

Here's a story: Sun and Microsoft agree to work together. While I'm in the news neighborhood, it looks like MS has a few more security issues.

Yesterday I needed to find all the files in two subdirectories (/tmp and /home) on a Linux platform ending with the characters ".java", that contained the string "commit". Here's the little ditty that made that happen:

find /tmp /home -type f -name "*.java" -exec grep 'commit' {} \; -print

For finding files and directories on Unix systems (or Cygwin) the find command rules.

 

Perl array/foreach FAQ: How do I perform an operation on each element in a Perl array? (Also written as, How do I use the Perl foreach operator, or What is the Perl foreach syntax?)

Software developers are always working with arrays. In Perl, that means that you're working with (a) normal Perl arrays or (b) Perl hashes (called 'associative arrays' before Perl 5.x).

When it comes to working with a regular Perl array (not a hash), here's a simple technique I often use to loop through the array, and perform an operation on each Perl array element:

Summary: How to use a Perl foreach loop to print every element in a Perl array.

To look at how to print every element in a Perl array using the foreach operator, the first thing we need is a sample array. Let's assume that you have an array that contains the name of baseball teams, like this:

@teams = ('cubs', 'reds', 'yankees', 'dodgers');

If you just want to print the array with the array members separated by blank spaces, you can just print the array like this:

Perl environment variables FAQ: How do I set environment variables in a Perl program?

In several other articles, we've demonstrated how you can access the value of environment variables from your Perl programs. For example, to determine the setting of your "PATH" environment variable, you can write a line of Perl code like this:

$path = $ENV{'PATH'};

As you may remember, "%ENV" is a special hash in Perl that contains the value of all your environment variables.

Perl file and directory FAQ: I'm often asked a question like this: "How do I do fill_in_the_blank for each file in a directory?"

I'll leave the "fill in the blank" part up to you. I've seen the need to do many things to every file in a directory - print each file, change one line in every file, rename the file - whatever. The cool part is that it's very easy to accomplish these tasks with Perl.

Here's a snippet of Perl code that prints a listing of every file in the current directory:

Summary: A quick Perl tip on how to list all files in a directory that match a given filename pattern, i.e., using the Perl filename "glob" pattern-matching syntax.

As a quick tip today, here's some sample Perl code that prints a listing of every file in the current directory that ends with the filename extension .html:

Perl URL FAQ: What is the easiest way to download the contents of a URL with Perl?

The easiest way I know to download the contents of a URL (i.e., "http://...") using Perl is to use the libwww-perl library, LWP.pm.

Once you have that Perl module installed, the code to download a URL looks like this:

Summary: A quick tip on how to read Perl command line arguments.

When you're writing a Perl script, command-line arguments are stored in the array named @ARGV.

$ARGV[0] contains the first argument, $ARGV[1] contains the second argument, etc.

$#ARGV is the subscript of the last element of the @ARGV array, so the number of arguments on the command line is $#ARGV + 1.

Here's a quick tabular list of Postgres commands related to listing information about a Postgres database. (Technically these are "psql commands", because you issue these commands from the psql command line program.)

These Postgres commands help you answer questions like "What tables are in this postgres database?", or "What databases do I have within Postgres?" (a handy question when it comes time for spring cleaning), and other questions. Other popular psql commands are related to permissions, indexes, views, and sequences.

Here's an Extreme Programming FAQ I found, from John Brewer and Jera Design. And while I'm spewing links, here's the Patterns Home Page. And one more link to A Learning Guide To Design Patterns.

Postgresql list FAQ: How do I get a list of databases in a Postgresql database?

Just log into your Postgresql database (with the psql command), and then run this command:

\l

(That's a backslash followed by the lowercase letter "L"). This command provides a nice list of all of the Postgresql databases.

I've been working on a Perl module that makes sending email messages a breeze, but one problem I had is that the Perl module MIME::Lite has a nasty way of failing when something goes wrong. I could not "catch" it's errors with die or warn, so after a little research, I found out how to deal with this problem using eval. According to one text, using eval is the only mechanism Perl has for exception handling.

Here's what I was doing that didn't work:

After trying some other approaches, I finally create a Perl program to extract lines from the middle of a file. Just give it the line number to start at, and a second line number to stop at, and this Perl program will print all the lines from the file in that range.

The source code for this Perl program is shown below.

Sample usage:

extract.pl 500 1000 myBigFile > smallerFile

Here's the code for extract.pl:

Ever need a program to extract some lines from the middle of a file, say lines 50-500 of a 10,000 line file? Well, I did, so I wrote one in Perl. Here's the code for "extract.pl, which lets me extract lines from a file by specifying the firstLine and lastLine that I want printed from a file: