1. Technology

Perl Absolute Beginners Guide - Lesson Two

Data Types

By

The heart of any programming language is it's ability to manipulate data. Computers and computer programs would be of very little use if there was no way to introduce our own data to them so that we can do some actual computing. Even something as outwardly simple as a calculator requires an interface so that you can provide it with some data to do computations (i.e. numbers and operators).

Programming in Perl is no different - Perl provides three basic data types that you can use to feed your program various types of data. In many languages (like C, C++, and Java) the term data type refers to a specific grouping of data, like integers, characters or decimals. Because you must specify the actual type of data you are working with in these languages, they are called strongly typed languages.

Perl Data Types

Perl is not a strongly typed language, in fact, Perl is the opposite - a loosely typed language. In Perl, there is no need to specify a type for your data, the Perl interpreter will choose the type based on the context of the data itself. When we talk about data types in Perl, we're actually talking about data structures.

All three of Perl's data types, or structures, are built on the first - the scalar. A scalar is a single unit of data. That data might be a number, a letter, a paragraph, or an entire web page - it could be anything, but only a single thing. A scalar is like a box. You label it, then you store some data in it. Take some time to read through the scalar tutorial as understanding how scalars work is key to learning the other Perl data types. All of them are build on the scalar.

A list is an ordered group of scalars. If a scalar is a box, think of a list as a nice neat row of those same boxes. You label the whole set, and you can put a collection of things inside it, or you can access each box individually to see which part of the set is inside. Once you've completed the scalar tutorial, it's time to move on to the list tutorial.

Finally, a hash is an unordered, indexed group of scalars that you access via special keys. This one is slightly more difficult to get a handle on, but in keeping with the box metaphor - think of a set of boxes, all in the same group - but rather than being in a nice neat row like a list, they are scattered around in no particular order. You can identify them by their key, which is like another label on the outside of the box. Read through the hash tutorial for a more detailed explanation.

Once you've completed the tutorials for all three data types, you're ready to explore some simple manipulation with operators.

  1. Data Types - Scalar Tutorial
  2. Data Types - List Tutorial
  3. Data Types - Hash Tutorial
  1. About.com
  2. Technology
  3. Perl
  4. Getting Started
  5. Perl Absolute Beginners Guide - Perl Data Types

©2014 About.com. All rights reserved.