Posts in the “perl” category

How to search a Perl list for a pattern using grep

A lot of times when you're using Perl you have a list (or array), and you need to search that list for all strings that match a certain regular expression pattern (or regex). Fortunately Perl is built for this type of work, and it's straightforward to (a) perform this search, and (b) put your search results in another list.

A perl function that runs a Unix command and returns its exit status

A perl function that runs a Unix command and returns its exit status

Here's the source code for a Perl function I created this morning. The purpose of this function/method is to run a specific Unix command, and then return the Unix exit status of that command. As usual with Unix and Linux systems, a zero exit status indicates success, and a non-zero exit status indicates some type of problem.

Without any further ado, here's the source code for my Perl function:

Perl for loop - How to do something for each element in an array

Perl for loop FAQ: What is the Perl for loop syntax? (Also written as, "How do I perform some type of operation on every element in a Perl array?)

Perl has a nice "for loop" syntax that lets you iterate through each element in an array, and perform some operation on each element.

Here's a simple Perl for loop example where I first create a small array, and then use the for loop to print each element in the array:

Perl modules FAQ - What to do when Perl modules aren't in their normal locations

Perl modules FAQ: I need to use a Perl module, but it isn't installed in the standard/default location, what can I do?

When you have root access to a Unix server, it's pretty easy to install Perl modules in their proper locations, and forget about them. But if you don't have root access and you need to install your Perl modules in non-standard directories, how will you get your programs to find your modules?

A Perl DBI DBH MySQL example

Perl MySQL FAQ: Can you share a Perl MySQL example? (Or, can you share a Perl DBI/DBH MySQL example?)

I don't know if this Perl DBI DBH MySQL example code will help anyone, but I thought I'd put it out here in hopes that someone will gain something from it.

Perl CGI form example - a CGI.pm scrolling list

Here's the source code for a Perl CGI scrolling list example. The CGI.pm module calls this a scrolling list, but it renders an HTML SELECT/OPTION form field with the "multiple" attribute.

The Perl code below shows how you can display an HTML form with a scrolling list field, using the Perl CGI.pm module. The first time this script is called it displays a scrolling list field in a form. After you submit the form, this script displays the item you selected in the scrolling list.

Here's the source code for this Perl CGI.pm scrolling list example script:

A Perl CGI (CGI.pm) textarea example

Here's the source code for a Perl CGI textarea example (a Perl form example). The Perl code below shows how you can display an HTML form with a textarea field, using the Perl CGI.pm module. The first time this script is called it displays a textarea field in a form. After you submit the form, this script displays the text that you entered in the textarea field.

Here's the source code for this Perl CGI.pm script, which generates a textarea in an HTML form:

A Perl CGI (CGI.pm) textfield form example

Here's the source code for a Perl CGI textfield example (a Perl form example). The Perl code below shows how you can display an HTML form with a textfield using the Perl CGI.pm module.

The first time this script is called it displays a textfield in a form. After you submit the form, this script displays the text that you entered in the textfield.

Here's the source code for this CGI.pm "Perl form" textfield example script:

A Perl array and foreach example

Perl array foreach FAQ: How do I loop over every element in a Perl array with a for loop (or foreach loop)?

A friend recently asked how to create a simple array of numbers with Perl, and also how to iterate over that list of numbers in a Perl foreach loop. In this article I'll share with you the code I shared with him.

Perl subroutine - how to return multiple values

Perl subroutines - multiple return values FAQ: Can you share some examples of how to return multiple values from a Perl subroutine?

Did you know that you can return multiple values from a Perl subroutine (function)? As a practical matter I haven't used this feature very much, but I've always thought it was an interesting programming language feature, very different from many other languages.

Perl hash introduction tutorial

Perl hash FAQ: Can you share some simple Perl hash examples?

Sure. If you're not familiar with them, a Perl hash is simply a Perl array that is indexed by a string instead of a number. A Perl hash is like a Map in the Java programming language, or an array in PHP.

Perl hash - Background information

To get started looking at a hash in Perl, let's look at a simple example. First, let's assume that Perl hashes don't exist. Next, lets assume that we need to store the prices of various food items you'll find in a restaurant.

The Perl unless operator

Perl unless FAQ: Can you share some examples of the Perl unless operator?

Perl has a cool keyword named "unless" that can make your code easier to read and write from time to time. The Perl unless operator is similar to the Perl "if" keyword ... but a little different.

A Perl unless/else example

Here's a quick example (inspired by one of the Star Trek movies) that shows how to use the Perl unless syntax. Hopefully it's fairly easy to read, in part due to the unless operator:

The Perl until loop examples and syntax

Perl until loop FAQ: Can you share some examples of the Perl until loop syntax?

The Perl until loop is similar to the Perl while loop, but essentially does the opposite thing. Where the Perl while operator will run this loop forever:

while ('true')
{
  # do stuff here
}

the Perl until loop will never run this loop:

Perl loop examples - The Perl foreach, for, while, and until loops

Perl loop FAQ: Can you share some "Perl loop" examples (Perl for loops, foreach loops, and Perl arrays)?

I've written a variety of "Perl loop" stories before, but I've never shown all the different Perl loop operators in one place before. In this Perl tutorial I'll try to provide a summary of the different Perl loop constructs.

A simple Perl array

Before showing any Perl loop examples, we'll need an array to work with. Here's a simple Perl array that contains five strings:

Perl subroutines - a Perl subroutine (sub) tutorial

Perl subroutines FAQ - As a developer, when you start working with subroutines in Perl, you'll probably have the same questions I did:

  • How do I define a Perl subroutine?
  • How do I call a Perl subroutine?
  • How do I pass arguments to a Perl subroutine?
  • How do I access arguments in a Perl subroutine?
  • How do I return a value from a Perl subroutine?

Perl hash - delete example

Perl hash delete FAQ: How do I delete an element (a key/value pair) from a Perl hash? Also, how do I delete multiple Perl hash elements at one time?

A Perl hash delete example - Delete an element by hash key

To delete a Perl hash element, use the Perl "delete" function. The general syntax of the Perl delete function looks like this:

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:

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

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: