Perl Data Types

Perl is a dynamically typed language, meaning that you don’t need to specify the data type of a variable explicitly. The Perl interpreter automatically assigns a type based on the context of the data. Perl has three fundamental data types: ScalarsArrays, and Hashes.

1) Scalars

scalar is a single data unit, which could be a string, number (integer or float), or a reference. Scalar variables are prefixed with a dollar sign ($). In Perl, scalars can hold any single value like a number, string, or a reference.

Scalar Operations:

Concatenation of string values using the . (dot) operator.
Mathematical operations on numeric values: +, -, *, /, %, **, etc.
Operations on scalar variables: +=, -=, .= (concatenate), ++, –, etc.

Integer Example:

 

String Example:

i) Print String Variables:

ii) String Concatenation and Arithmetic:

 

Output:

 

iii) Alternative Delimiters:

 

 

iv) String Functions:

 

 

v) String Indexing:
vi) Escaping Characters:

 

 

Output:

 

vii) Using qand qq Operators:

 

 

 

viii) Chop and Chomp Functions:

 

chop() removes the last character from a string regardless of what that character is.
chomp() removes any newline character at the end of the string.

 

 

2) Arrays:

An array in Perl is an ordered list of scalars. Array variables are prefixed with the @ symbol.

Array Example:

 

 

3) Hashes:

hash in Perl is an unordered collection of key-value pairs. They are prefixed with a % symbol and accessed via keys.

Hash Example:

 

23 thoughts on “Perl Data Types”

Leave a Comment

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

error: Content is protected !!
Scroll to Top