• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / Perl and CGI Scripting / Data Types in Perl

Data Types in Perl

3 September 2005 / Leave a Comment

Like most programming languages, Perl knows about different varieties of data. In this tutorial we’ll explore the most common data types used in Perl:

  • Scalars
  • Arrays (lists)
  • Associative arrays (hashes)

Scalars

Scalar variables are used to store single values – for example, a number or a string. They are preceded by the dollar sign ($). Here are some examples of scalars:


$first_name = "Jacob";
$total_widgets = 43;
$price = 12.99;

Unlike many other languages, Perl does not distinguish between the storage of strings, integers and decimal numbers. They are all just scalars as far as Perl is concerned.

Instead, the context in which you use a scalar determines how it will be interpreted. For example, if you are joining two scalars together using the string concatenation operator (.), the scalars will be interpreted as strings. On the other hand, if you add two scalars using the addition operator (+), Perl will try to interpret the scalars as numbers.

Arrays (lists)

Arrays, often called lists, can store a list of many scalar values at once. They are preceded by the at sign (@). Here are some arrays in Perl:


@names = ( "Mary", "Jim", "Fred" );
@salaries = ( 54000, 22500, 45000 );
@prices = ( 14.99, 19.99, 29.99, 39.99, 59.99 );

You can access a single element in an array using the numeric index (position) of the element. In Perl, the first element of an array has the index 0, not 1 as you might expect.

Here are some examples, using the arrays we defined above:


print $names[0];

(prints “Mary”)


print $salaries[1];

(prints “22500”)


print $prices[2];

(prints “29.99”)

Notice that each array element (e.g. $names[0]) uses a $ symbol rather than an @ symbol. This is because each element is a scalar, not an array.

Slicing an array

We can also pull out several elements of an array in a row, to form a smaller array. This is known as extracting a slice from an array. For example:


@slice = @prices[2..4];

(@slice now holds the values ( 29.99, 39.99, 59.99 ) ).

The code @prices[2..4] means “give me elements 2 to 4 inclusive from the array @prices“.

Associative arrays (hashes)

The final data type we’re going to cover here is the associative array, often called a hash. Associative arrays are similar to normal arrays, with one important difference. Instead of using numbers to index each element in an array, you can use more meaningful strings.

Hash variables are preceded by the percent sign (%).

Here are some examples of hashes:


%favourite_cats = ( mary => "Fluffy", jim => "Tibby", fred => "Lucky" );

%colour_purple = ( r => 255, g => 0, b => 255 );

%prices = (
  "Super Widget" => 39.99,
  "Wonder Widget" => 49.99,
  "Mega Widget" => 69.99
  );

Some things to note:

  • First of all, each element (e.g. "Fluffy") is referenced by a string index (e.g. mary). This index is often called a key, and the corresponding element is called a value.
  • When defining a hash using the key => value syntax above, you don’t need to put quotes around the key, unless there are spaces or other non-word characters in the key (as in the %prices example).
  • For readability, you can put each key/value pair on a separate line, as shown in the %prices example above.

To access values in a hash, you use the syntax $hash{$key}. For example:


print $favourite_cats{mary};

(prints “Fluffy”)


print $colour_purple{g};

(prints “0”)


print $prices{"Mega Widget"};

(prints “69.99”)

As with arrays, notice that the hash values are scalars, so they start with a $ (for example, $favourite_cats{mary}).

Filed Under: Perl and CGI Scripting Tagged With: cgi, cgi-bin, numbers, perl, strings, variable types, web programming

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits