Posts in the “perl” category

A Perl temp file example

Many times in Perl applications you need to be able to create a temporary file (a Perl temp file). In this scenario you don't want to get into the system-specific details of temporary directories and things like that; you just want to create and use a temporary file.

Here are some Perl code snippets that should help get you started down this road.

Perl stat - How to determine the access or modification time of a file

How to use the Perl stat function.

Problem: Using Perl, you need to determine the last time a file was accessed (read) or updated (modified).

Solution: Use the Perl stat function to get this information. Let's look at a couple of short examples to see how this works.

Perl stat - getting the file access time

To determine the last access (read) time of a file named foo.txt, use this sample Perl code:

Perl stat - How to get the size of a file

Perl FAQ: How do I determine the size of a file from within a Perl program/script?

Solution -  the Perl stat function

Just use the Perl stat function to get the file size. Here is a quick example.

Example - get the file size with Perl stat

To determine size of a file named foo.txt, just use a little Perl code like this:

Perl file date tests - How to test when a file was last accessed or modified

Perl has a couple of convenient file operators that let you determine when a file was last accessed or modified in units of days. These operators are:

-M  The modification age of the file, in days
-A  The access age of the file, in days

A quick Perl file test example

You can use these Perl file test operators in your scripts to determine when files are getting "old" (where the definition of "old" is up to you).

Perl - How to process every file in a directory that matches a pattern

Perl FAQ: "How can I process every file in a directory that matches a certain filename pattern?"

There are several ways to do this, but I normally use the glob function, because I can remember the syntax pretty easily.

Let's look at a couple of glob examples.

Using the glob operator

You can use the glob operator to get a list of all files in the current directory like this:

How to concatenate strings in Perl

A Perl FAQ is "How do you concatenate (merge) two or more strings in Perl?"

Use the "." operator

The short answer is that you use the . operator. Here's a simple example:

$name = "alvin" . " " . "alexander";

Of course I could have also done that like this:

$name = "alvin " . "alexander";

but I wanted to show an example with more than two strings.

Perl sort array example - How to sort a Perl string array

Summary: How to sort a Perl string array (when you don't have to worry about whether the strings are all uppercase or lowercase).

Perl excels at a number of tasks, especially text-processing, and if you have an array of strings, and all of the elements in the array are either uppercase or lowercase, sorting your string array is very easy. In this tutorial I'll show how to sort an array of strings, and will also show how to sort your string array in reverse order.

Perl substring - How to search for one string in another string

Perl substring FAQ: Can you demonstrate some Perl substring examples?

As a language, Perl is very good at text processing, including dealing with strings and substrings. In this article we'll take a look at some Perl substring manipulations we can perform.

For the purposes of our Perl substring tutorial, let's assume that we have a string defined like this:

Perl string array - How to create and use an array of strings

With Perl we work with strings and arrays (or lists) of strings all the time. I thought I'd take a few moments here to show the most common ways to create a Perl string array, and also to loop through (or iterate through) the list of strings I create.

How to create a Perl string array

When you first start to work with Perl, you might create a string array like the following example:

Perl substr example - How to extract a substring from a string

Perl substring FAQ: How do I extract a substring from a string?

Solution: Use the Perl substr function. The general substr syntax looks like this:

$my_substring = substr($full_string, $start_pos, $length);

Perl substring example (substr)

A more specific Perl substring example is shown in the following code, where I extract the $firstname and $lastname strings from the $name string:

Perl environment variables - How to access Perl environment variables

Here's a quick example program that demonstrates how to access environment variables from within your Perl programs:

# environment variables are held in the %ENV hash
foreach $key (sort keys %ENV)
{
  print "$key is $ENV{$key}\n";
}

Using this simple Perl foreach loop, here's a subset of the output this script prints on my MacBook Pro:

A free Perl editor - Komodo Edit

Free Perl editors FAQ: Can you recommend any good free Perl editors?

I had to get back into some Perl development for a recent project, and when I realized the project was going to take a few days to complete, I decided to look around to see if I could find a good free Perl editor.

Perl array examples

Perl array examples FAQ: Can you share some Perl array examples (Perl array programming examples)?

After doing a lot of work with Perl arrays recently, I thought I'd post a little collection of Perl array examples out here today. The Perl array syntax isn't too hard, but there are a few common mistakes you can make, so hopefully a simple reference page will help.

Perl hash add element - How to add an element to a Perl hash

Perl hash "add" FAQ: How do I add a new element to a Perl hash? (Or, How do I push a new element onto a Perl hash?)

The Perl hash is a cool programming construct, and was very unique when I was learning programming languages in the late 1980s. A Perl hash is basically an array, but the keys of the array are strings instead of numbers.

Basic Perl hash "add element" syntax

To add a new element to a Perl hash, you use the following general syntax: