perl

recent posts related to the perl programming language

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:

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.

Perl code to read one line from a text file

When I work with Perl I'm often performing a task where I need to read from a text file, and many times all I have to do is read one record from the file. This happened again today, where I have a text file that just contains one pid (process id), and I just need to get that pid right before I do some other work.

Perl - How to compare a string against multiple patterns

For a Perl program that I'm working on right now, I need to match the elements of an array of strings against several patterns, and then take an action if the current string matches one of my patterns. This is pretty easy in this case, in part because it's easy to match a string against multiple patterns in Perl, and also because my patterns are very simple -- no regular expressions involved.

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:

Perl true and false - What is true and false in Perl?

Perl true - false FAQ: What is true in Perl? What is false in Perl?

The Perl programming language is a little unusual in not having true and false boolean operators. Because of this -- and my advancing age -- I can never seem to remember what equates to true and false in Perl, so I decided to create this page.

Perl true false summary

In short, the following elements evalue to false in Perl:

Perl comparison operators

Perl equality FAQ: Can you share a list of the Perl equality operators?

Sure. Here's a convenient list of the Perl comparison operators (also known as Perl equality operators, equal, or not equal operators).

The Perl comparison operators are different for numeric and string comparison tests, as you can see in the following table:

How and why to lock data files with Perl

Two of the Perl questions we're often asked are:

  • "Why should I lock my data files when I'm writing to them?"
  • "How do I lock my data files with Perl?"

Here's a quick tutorial that demonstrates both the "why" and "how" of data file locking with Perl.

Put Perl test data in the same file as your source code

Perl test data FAQ: How can I store some sample/test data with my source code in Perl?

Answer: With Perl it's very easy to store some sample data in the same file as your Perl source code. Assuming you're creating a "main" Perl program (not a Perl module) you can use the special __END__ operator, and then include your data after that operator, as shown in my sample code below. You can then access that data using the main::DATA operator inside of a while loop.

Here's a quick sample program:

How to access arguments to a Perl subroutine or function

Perl question: How do I access the arguments that have been passed to my subroutine or function?

Answer: The special array @_ holds the values that are passed into a subroutine/function, and you use that array to access those arguments. The first argument is represented by the variable $_[0], the second argument is represented by $_[1], and so on.

It may also help to know that number of elements passed in to your subroutine is given by the value of scalar(@_).

Syndicate content