recent posts related to the perl programming language

A Perl trim function

Perl string trim FAQ: Is there something like a "trim" function in Perl, similar to the Java trim function, which trims leading and trailing whitespace characters from a string?

A Perl trim function

My Perl skills aren't exactly up to date these days, but in days gone past there was no Perl trim function, and somewhere along the way I wrote one. Here's the source code for my Perl trim function:

A Perl program to determine RSS readers from an Apache access log file

Perl/RSS FAQ: How many RSS subscribers do I have on my website?

Like many other people with a blog or website, I was curious yesterday about how many RSS readers/subscribers the devdaily website has. You can try to get this information in a variety of ways, but the real information is on your server, in your Apache log files.

To figure out how many RSS subscribers your website has, just go through your Apache log file, find all the records that look like this:

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 uppercase and lowercase string conversion

Perl lowercase/uppercase string FAQ: How do I convert a string to uppercase or lowercase in Perl?

Solution: To convert a string to all uppercase characters use the Perl uc function, and to convert them to lowercase use the lc function.

Here are a couple of examples to help demonstrate this Perl uppercase/lowercase string conversion.

Perl uppercase string conversion

Here's a Perl uppercase example, converting a Perl string from whatever it was to all uppercase characters:

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

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.

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.

Perl hash size - How to get the size of a hash in Perl

Perl hash size FAQ: How do I get the size of a hash in Perl?

Short answer: To get the size of a Perl hash (the Perl hash size), use the Perl "keys" function, and assign it to a scalar value, like this: