Posts in the “perl” category

My Perl hash tutorials

Perl hash tutorials FAQ: Can you share some Perl hash tutorials, or Perl hash examples?

I've recently written a number of articles about the Perl hash construct (or "Perl array hash"). In an effort to try to organize the Perl hash tutorials I've written, I've created this article to help link them all together.

The Perl exists function - test to see if a hash key exists

Perl "hash key exists" FAQ: How can I test to see if a key exists in a Perl hash?

Many times when working with a Perl hash, you need to know if a certain key already exists in the hash. The Perl exists function lets you easily determine if a key already exists in the hash.

Perl string length FAQ

Perl string length FAQ: How do I get the length of a Perl string?

To get the length of a string in Perl, use the Perl length function, like this:

# perl string length example
$size = length $last_name;

The variable $size will now contain the string length, i.e., the number of characters in the variable named $last_name.

Another Perl string length example

I just thought to write this article out here when I ran across the following Perl string length code:

Perl loop - how to break out of a loop in Perl

Perl loop break FAQ: How do I break out of a Perl loop?

Problem: You're writing some Perl loop code (for, foreach, or while), and you have a condition where you need to break out of your loop early, and you quickly find out that Perl doesn't have a 'break' operator.

The Perl "break" statement

In many programming languages you use the break operator to break out of a loop like this, but in Perl you use the last operator to break out of a loop, like this:

Perl next operator - for loop and if statement examples

Perl next loop FAQ: Can you demonstrate how to use the Perl next operator in a for loop?

Problem: You're writing code for a Perl loop, and you need to write some logic to skip over the current element in the loop, and move on to the next loop element.

Perl string character processing - How to process every character in a string

Perl string processing FAQ: How can I process every character in a Perl string?

I recently had to write some Perl code to process every word in a file, and that made me wonder how to process every character in a Perl string. I didn't know how to do this, but I just cracked open my copy of the Perl Cookbook, and found a couple of possible solutions.

Perl grep array FAQ - How to search an array/list of strings

Perl "grep array" FAQ: Can you demonstrate a Perl grep array example? (Related: Can you demonstrate how to search a Perl array?)

A very cool thing about Perl is that you can search lists (arrays) with the Perl grep function. This makes it very easy to find things in large lists -- without having to write your own Perl for/foreach loops.

Apache access log - A Perl program to read an Apache access log file

Here's some free source code for a Perl program I've created to read and analyze standard Apache access log files and print out the most popular pages. As shown below it will print out the top 100 popular pages on your website, but that is easily changed.

The code could use a lot of improvement, and I will continue to post updates here when I make improvements. Other than that it's provided here free of charge, so feel free to make your own improvements to it as well.

Checking to see if a Perl module is in your @INC path

As I started working on a new Unix system yesterday I needed to know if that system had certain Perl modules installed on it that I needed. In my case the specific Perl modules I needed were SOAP::Lite and the Error module.

One way to find out was to just install my Perl program and then try to run it, but because I knew I needed these modules beforehand I just ran the following commands to see if these modules were installed on this HP-UX system:

perl -e 'use SOAP::Lite;'

and

Perl flock - file locking with Perl

Perl flock FAQ: Can you show me how to lock a file with the Perl flock function?

Sure, here are a couple of quick file-locking examples with Perl and the Perl flock function.

Perl flock example #1

In the first Perl flock example I'll show how to lock a file, but I'll intentionally do it in a bad way, and lock a file for roughly 20 seconds while I write records to it and sleep in between the writes:

Perl subroutine values - How to return multiple values from a Perl function

Perl subroutine FAQ: How do I return multiple values from a Perl subroutine (Perl function)?

One of the things I really like about Perl is that you can return multiple values from a function (and you don't have to create some type of artificial class to encapsulate them). Here's the basic way to return multiple values from a function/subroutine named foo:

A Perl module template (template code for a Perl module)

If you've ever needed some template/boilerplate code for creating your own Perl module, here's some sample code intended to give you just what you wanted. Feel free to copy and paste the Perl code shown below and use it as a template for creating your own modules.

How to use this sample Perl module

If you've never created your own Perl module I recommend first seeing how this sample code works. Here's all you have to do to get it running:

Use File::Basename to separate a filename from its directory

Question: Using Perl, if I have a string that includes the full path to a file (i.e., it includes both the full path of the directory and the filename), how do I split the string into its directory and filename components?

Answer: Use the basename and dirname methods of the Perl File::Basename module.

The following example shows how to break this string '/Users/al/work/file1.pdf' into its directory and filename components:

How to add an item to a Perl hash

Perl hash FAQ: How do I add an item/element to a Perl hash?

Answer: Adding an item to a Perl hash is just like creating a Perl hash initially. Just assign the new item to the hash with the correct syntax, and you're in business.

In the following sample code I show how this is done by creating a hash named prices. The hash key is the name of a product, like pizza, and the hash value is the price of that product. Here's my Perl hash sample code:

A Perl hash print example (printing hash elements)

Perl hash question: How do I print the elements stored in a hash in Perl?

Answer: There are at least two ways to print the elements of a Perl hash, including one way that I think is easy to remember, and another way that is recommended if you have a very large hash.

Perl hash printing with a foreach loop

The easier way for me to remember is with a Perl foreach loop. In the following sample code I'll first create a Perl hash, and then I'll print out each key and value stored in the hash:

Perl hash remove - How to remove an item from a Perl hash

Perl FAQ: How do I remove an item from a hash?

Answer: Use the Perl delete function.

The general syntax you need to use is shown here:

delete($hash_name{$key_name});

If you'd like more details and examples, read on...

Perl hash - remove/delete element example

Here's a complete example where I show both how to create and print a Perl hash, and then show how to remove elements from the hash using the Perl delete function:

Perl hash foreach and while - How to loop over a hash in Perl

Perl hash question: How do I traverse the elements of a hash in Perl?

Answer: There are at least two ways to loop over all the elements in a Perl hash. You can use either (a) a Perl foreach loop, or (b) a Perl while loop with the each function. I find the Perl foreach syntax easier to remember, but the solution with the while loop and the each function is preferred for larger hashes.

Perl array size/length - How to determine the size of a Perl array

A frequently asked question Perl question is "How do I determine the size/length of a Perl array?", or the equivalent "How do I determine how many elements are in a Perl array?"

There are at least three different ways to do determine the Perl array size/length. Here's some Perl source code that shows these three Perl array size approaches: