perl

recent posts related to the perl programming language

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

A Perl random integer example

Perl random integer FAQ: Can you provide a Perl random integer example?

In my earlier Perl random tutorial, I described several different ways of generating random numbers in Perl. Today I'd like to share a Perl script I wrote that uses a random integer number to rotate files on this website.

A Perl random integer script example

The following Perl script performs the following functions:

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:

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 hash add element - How to add an element to a Perl hash

Perl hash "add" FAQ: How do I add a new element to a Perl hash? (Or, How do I push a new element onto a Perl hash?)

The Perl hash is a cool programming construct, and was very unique when I was learning programming languages in the late 1980s. A Perl hash is basically an array, but the keys of the array are strings instead of numbers.

Basic Perl hash "add element" syntax

To add a new element to a Perl hash, you use the following general syntax:

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:

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.

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.

Syndicate content