Data Types and Variables in Perl - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Data Types and Variables in Perl

Share This

In this language, we don't need to specify type of the data in your program, as Perl is loosly typed language. The Perl interpreter will manage the data type based on the context.

A variable can store numeric value or string (a sequences of characters). Here, a string is alphanumeric value enclosed in either single ' or double " quotes.

Note that the double-quoted string literals allow variable interpolation, and single-quoted strings do not. Like most of the programming languages, it also supports escape sequence using \ symbol and special symbols like newline \n , tab \t etc. A few samples are shown in the following code.

#!/usr/bin/perl $str = "This is sample \nProgram!"; print "$str\n"; $str = 'This is sample \nProgram!'; print "$str\n"; $str = "This is \usample Program!"; print "$str\n"; $str = "This is \Usample Program!"; print "$str\n"; $str = "This is \Usample\E Program!"; print "$str\n";

There are three basic data types, these are scalar, array, and hash. These are discussed below:

Scalar It's a simple variable (can be a number, a string, or a reference), which is preceded by $ symbol.
Array It's an ordered list of scalars. The variable is preceded by @ symbol.
Hash It's an unordered set of key/value pairs. The hash variable is preceded by % symbol.

In Perl same name can be used for variables of different data types as every variable type in maintained in separate namespace.

Scalar Variable

A scalar data might be an integer, a floating point, a character, a string, a paragraph, or content of a web page.


#!/usr/bin/perl $rollno = 2; $name = "Wolski"; $avgmarks = 45.75; print "Roll = $rollno\n"; print "Name = $name\n"; print "Average Marks = $avgmarks\n";

Array Variable

To refer to a single element of an array the $ symbol is used with the variable name followed by the index of the element in square brackets as shown in the following example.


#!/usr/bin/perl @ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n";

Hash Variable

The following program shows how to work with key/value pairs.


#!/usr/bin/perl %data = ('John', 45, 'William', 30, 'Anand', 40); print "\$data{'John'} = $data{'John'}\n"; print "\$data{'William'} = $data{'William'}\n"; print "\$data{'Anand'} = $data{'Anand'}\n";


Variable Context in Perl

Perl treats same variable differently based on Context as shown below.


#!/usr/bin/perl @names = ('John', 'Benjamin', 'Jacob'); @copy = @names; $size = @names; print "The names are : @copy\n"; print "Number of names is : $size\n";

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.