BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Data Types in Perl

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Perl

Perl has three data types: Scalars, arrays of scalars and hashes (also known as associative arrays, dictionaries or maps). In perl we need not to specify the type of data, the interpreter would choose it automatically based on the context of data.

For e.g. In the following code, I am assigning an integer and a string to two different variables without specifying any type. The interpreter would choose age as integer type and name as string type based on the data assigned to them.

#!/usr/bin/perl
$age=29;
$name="Chaitanya";

Scalars

Scalars are single data unit. It can be a number, float, character, string etc. In perl, they are prefixed by a dollar sign($). Read about them in detail here.
For example:

$num1=51; 
$str1="beginnersbook"; 
$num2=2.9;
$str2="hello";

Arrays

They are ordered list of scalars, prefixed with “@” sign. Index starts with 0 which means to access the first element of array, we need to use index value as 0 (zero). For eg.

#!/usr/bin/perl
@pincode = (301019, 282005, 101010);
print "\$pincode[0] = $pincode[0]\n";

Output:
pincode[0]= 301019

If you are wondering about the backslash (\) used in the print instruction, it is used to avoid the interpolation of first occurrence of pincode[0]. You can read more about arrays here.

Hashes

They are unordered group of key-value pairs. They are prefixed with percent(%) sign.

#!/usr/bin/perl

%ages = ('Chaitanya', 29, 'Ajeet', 28, 'Tom', 40);

Here “ages” is a hash that has key value pairs. where key is a name and value is age. To access the age of Ajeet we use $age{‘Ajeet’} instruction.
Read more about hashes with examples here.

❮ PreviousNext ❯

Top Related Articles:

  1. Perl Syntax
  2. Use strict and use warnings in Perl
  3. Perl – Lists and Arrays
  4. Installing Perl on Windows, Mac, Linux and Unix
  5. Perl Tutorial for beginners

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap