perl

recent posts related to the perl programming language

Perl regex search and replace in many files

Perl regex search/replace FAQ: How do I replace every TAB character in a file with a comma? (More generally, how do I search for a Perl regex pattern, and replace it with a different string?)

Perl regex search and replace

Replacing each TAB character in a file with a comma character can be accomplished several different ways with Perl. The key in any approach is to perform a Perl regex search and replace operation.

(Note: Please make a backup of your file(s) before continuing.)

Perl array and foreach - How to operate on each Perl array element

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:

How to print a Perl array

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 - How do I set environment variables?

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 directory faq - How to work on each file in a directory

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:

Perl directory list - List all files that match a filename pattern

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 - an easy way to download URL contents

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:

How do I read command-line arguments with Perl?

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.

Perl exception handling

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:

Perl file example - A program to extract lines from a file

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:

Syndicate content