Posts in the “perl” category

Perl ‘equals’ FAQ: 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, I can never seem to remember what equates to true and false in Perl, so I decided to create this page.

What is true/false in Perl

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

Perl array printing: How do I print the entire contents of an array with Perl?

Perl array FAQ: How do I print the entire contents of an array in Perl?

To answer this question, we first need a sample Perl array. Let's assume that you have an array that contains the name of baseball teams, like this:

@teams = ('cubs', 'reds', 'yankees', 'dodgers');

Perl array printing

Now, if you just want to print the array with the array members separated by blank spaces, you can just print the array like this:

Perl “exec”: How to execute system commands

Summary: Using the Perl backtick operator to execute system comands

One of the real strengths of the Perl programming language is the ease in which you can access (execute) the commands of the underlying operating system. As other programming languages point out, this can be considered a weakness, because it also makes your programs system-dependent, but in reality I've found this to be a problem only five or 10 percent of the time. 

Perl array copy example: How to copy an array in Perl

Perl array copying FAQ: Can you share some examples of how to copy a Perl array?

Copying a Perl array is actually so simple it seems like a trick question, but unfortunately it's one of those things that is only easy once you know how to do it. (Personally I find the syntax a little non-standard.)

A Perl copy array example

To copy a Perl array named @array1 to a Perl array named @array2, just use this syntax:

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 split function - how to read a CSV file

Problem - the CSV file

You have a file that contains columns of data, and each column is separated by a comma (a CSV file). The width of each column can vary, but the column data is guaranteed not to contain any columns itself, so it's safe to think of the comma as truly being a column separator. So, how do you read the data?

Solution - the Perl split function

Okay, so what you're saying is that you have CSV file data that looks like this:

Perl random numbers (tutorial, examples)

Perl random number FAQ: Can you show me some examples of how to get a random number in Perl?

Perl random number - solution

In its simplest form, if you just need a random decimal number between 0 and 1.0, you can use the Perl rand function like this:

# generate a random number in perl with the rand function

my $random_number = rand();
print $random_number, "\n";

When I save this Perl random number code to a file and run it three times, I get these results:

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 - How to read from a named pipe (fifo)

Perl FAQ: How can I write a Perl script to read from a named pipe (FIFO file)?

Here’s some code from a Perl program where I open up a named pipe (a FIFO file), then read data from that file until the end of time ... or at least until someone kills this program.

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 case-insensitive string array sorting

Perl string array sorting FAQ: How can I sort a Perl string array in a case-insensitive manner?

I mentioned in my first Perl array sorting tutorial that the default Perl sorting algorithm sorts characters by their ASCII values. Because of that, my simple Perl array sorting example using the following strings worked just fine:

# a simple perl string array
@pizzas = qw(pepperoni cheese veggie sausage);

However, if I add a few uppercase strings to that string array, like this:

Perl error: Can't locate module in @INC

From a recent email: Help, I've just run into this Perl error: "Can't locate module in @INC".

Solution: If you get a Perl error message like "Can't locate in @INC", this message means that the Perl module you're trying to include (like the module named Foo) can't be found in Perl's include path, which is represented by the variable named @INC.

How to list Perl CGI environment variables

Perl CGI environment variables: How can I print all of the Perl CGI environment variables? I'd like to see what CGI environment variables are available, and what their values are.

Somewhere in my history of working with Perl and CGI programs, I decided I needed a program that would simply print all of the CGI environment variables that my Perl programs were aware of. I can't remember if it was curiosity or what, but I decided that I needed a CGI program that did nothing else but print the environment variables it knew of.

Perl printing examples

Perl printing FAQ: Can you share some Perl printing examples?

There are several different ways to print in Perl, and I thought I'd share some examples here today.

The Perl print function

Generally you'll print simple output with the Perl print function. As a simple example, you can print a string literal using the Perl print function, like this:

Perl: How to access shell environment variables through Perl associative arrays

Perl shell environment variables FAQ: How can I access Unix shell environment variables from a Perl script?

One of the great features of the Perl language is it's support of associative arrays. Unlike normal arrays, whose subscripts can only be integers, the subscripts of associative arrays are text strings. This may not sound like much yet, but with associative arrays (or hashes as they're now called) we can create fairly complex data structures with Perl.

Perl best practice: prefer foreach to for

Perl problem: When you don't use Perl every day, it's hard to remember the combination of the for loop syntax and how to deal with the special $_ variable to refer to each element within the body of the loop.

Solution: As I write this today, I still can't remember how to get this for loop and $_ syntax to work, at least not without pulling out a book, or looking it up on the internet. A much better solution is to just use Perl's foreach syntax, and never look back.