Posts in the “perl” category

Perl directories - 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 - 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.

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 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:

Use Perl here documents to print multiple lines of output

Summary: How to use Perl here documents, i.e., the Perl heredoc syntax.

Perl offers a convenient way of printing multiple lines of output through an interesting feature known as a "Perl here document". A here document, or heredoc, lets you easily store multiple lines of text in a Perl variable.

A multiline Perl here document works like this:

Perl regular expressions: Common escape sequences

You can do many crazy things with Perl regular expressions, but many times you just need to use an escape character (or escape sequence) in a regular expression. I'm often asked what escape sequences you can use in Perl regular expressions, so without any further ado, here is a simple list of "special" characters (such as the [Tab] character) can be matched by the Perl regular expressions:

A sample Perl CGI program that can be used to edit files on a web site

Below I've included a sample Perl CGI program that I use to edit some files on my web sites. I've modified the file a little bit for the purposes of this example, but it's essentially what I use.

Very Important: This program by itself is not secure in any way. It does not require a user login, etc. At the very least you will want to secure access to this program with an Apache htaccess configuration, or something similar on other Perl CGI web servers. This program is shown here for demonstration purposes only.

Perl CGI environment variables example

Here's a sample Perl CGI program I wrote that prints out all of the environment variables it knows. I've found this program to be very useful when first installing a web server, or when debugging a problem with a web server or new environment.

Perl CGI environment variables - example program

Here's the code:

Perl include path - how to print the Perl environment include path

Help, I need to install a Perl module, and I need to know what my Perl "include" path (library path) is?

Here's a simple way to print your Perl include path from the command line:

perl -e "print qq(@INC)"

You can just run that command from the Unix/Linux or DOS command line. The output I get from this command on my Windows PC looks like this:

C:/Perl/lib C:/Perl/site/lib .

The output I get on a nearby Linux server looks like this:

Perl Excel example - A Perl program to parse Microsoft Excel XLS files

A co-worker, Chris Smith, created a very elegant Perl program to parse a Microsoft Excel XLS file for me some time ago. I recently modified that program to convert what was essentially a "glossary" in an Access database.

I first saved the Access database table as an Excel file. Then I used the following Perl program to extract the contents of the second and third columns from that file, and write them out to another file. The output file was in a Wiki (technically TWiki) format.

Perl array example - search replace in a Perl array

I was just modifying a Perl program so I could use a regular expression (regex) to search a Perl array for all less-than (<) and greater-than (>) symbols, and replace those with their HTML equivalents tags ("&lt;", and "&gt;", respectively).

Here's the source code I created to perform this Perl array search and replace operation (a Perl replace regex operation):

Perl reverse - Reverse file contents with Perl

Working on a Unix system, I just needed to reverse the contents of a file, and thought I'd show how I ended up doing it.

My file-reversal needs

For my situation I needed to (a) get 10 lines from the end of a file, (b) reverse those ten lines, and (c) save them to another file. If my Unix system supported the -r option of the tail command this would have been a no-brainer, but it didn't, so I had to work a little harder.

How do I set environment variables in Perl programs?

Perl environment FAQ: How do I set environment variables in Perl programs?

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 just do something 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.

A Perl function to prompt users during interactive command-line input

For many Unix users, Perl has replaced the Bourne shell, C shell, and Korn shell as a preferred programming language for many scripting duties and short programs.

Used as a scripting language, Perl programs often require you to prompt the user to enter some type of input. I've found that instead of writing separate "prompting" code in each of my Perl scripts, it's easier to create one prompting routine that can be used in almost all Perl programs.