- Perl - Sending Email
- Perl - Regular Expressions
- Perl - Coding Standard
- Perl - Special Variables
- Perl - Error Handling
- Perl - Directories
- Perl - File I/O
- Perl - Formats
- Perl - References
- Perl - Subroutines
- Perl - Date & Time
- Perl - Operators
- Perl - Loops
- Perl - IF...ELSE
- Perl - Hashes
- Perl - Arrays
- Perl - Scalars
- Perl - Variables
- Perl - Data Types
- Perl - Syntax Overview
- Perl - Environment
- Perl - Introduction
- Perl - Home
Perl Advanced
- Perl - Functions References
- Perl - Embedded Documentation
- Perl - Process Management
- Perl - Packages & Modules
- Perl - CGI Programming
- Perl - Database Access
- Perl - Object Oriented
- Perl - Socket Programming
Perl Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Perl - Scalars
A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page.
Here is a simple example of using scalar variables −
#!/usr/bin/perl $age = 25; # An integer assignment $name = "John Paul"; # A string $salary = 1445.50; # A floating point print "Age = $age "; print "Name = $name "; print "Salary = $salary ";
This will produce the following result −
Age = 25 Name = John Paul Salary = 1445.5
Numeric Scalars
A scalar is most often either a number or a string. Following example demonstrates the usage of various types of numeric scalars −
#!/usr/bin/perl $integer = 200; $negative = -300; $floating = 200.340; $bigfloat = -1.2E-23; # 377 octal, same as 255 decimal $octal = 0377; # FF hex, also 255 decimal $hexa = 0xff; print "integer = $integer "; print "negative = $negative "; print "floating = $floating "; print "bigfloat = $bigfloat "; print "octal = $octal "; print "hexa = $hexa ";
This will produce the following result −
integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = 255
String Scalars
Following example demonstrates the usage of various types of string scalars. Notice the difference between single quoted strings and double quoted strings −
#!/usr/bin/perl $var = "This is string scalar!"; $quote = I m inside single quote - $var ; $double = "This is inside single quote - $var"; $escape = "This example of escape - Hello, World!"; print "var = $var "; print "quote = $quote "; print "double = $double "; print "escape = $escape ";
This will produce the following result −
var = This is string scalar! quote = I m inside single quote - $var double = This is inside single quote - This is string scalar! escape = This example of escape - Hello, World
Scalar Operations
You will see a detail of various operators available in Perl in a separate chapter, but here we are going to pst down few numeric and string operations.
#!/usr/bin/perl $str = "hello" . "world"; # Concatenates strings. $num = 5 + 10; # adds two numbers. $mul = 4 * 5; # multippes two numbers. $mix = $str . $num; # concatenates string and number. print "str = $str "; print "num = $num "; print "mul = $mul "; print "mix = $mix ";
This will produce the following result −
str = helloworld num = 15 mul = 20 mix = helloworld15
Multipne Strings
If you want to introduce multipne strings into your programs, you can use the standard single quotes as below −
#!/usr/bin/perl $string = This is a multipne string ; print "$string ";
This will produce the following result −
This is a multipne string
You can use "here" document syntax as well to store or print multipnes as below −
#!/usr/bin/perl print <<EOF; This is a multipne string EOF
This will also produce the same result −
This is a multipne string
V-Strings
A pteral of the form v1.20.300.4000 is parsed as a string composed of characters with the specified ordinals. This form is known as v-strings.
A v-string provides an alternative and more readable way to construct strings, rather than use the somewhat less readable interpolation form "x{1}x{14}x{12c}x{fa0}".
They are any pteral that begins with a v and is followed by one or more dot-separated elements. For example −
#!/usr/bin/perl $smile = v9786; $foo = v102.111.111; $martin = v77.97.114.116.105.110; print "smile = $smile "; print "foo = $foo "; print "martin = $martin ";
This will also produce the same result −
smile = ☺ foo = foo martin = Martin Wide character in print at main.pl pne 7.
Special Literals
So far you must have a feepng about string scalars and its concatenation and interpolation opration. So let me tell you about three special pterals __FILE__, __LINE__, and __PACKAGE__ represent the current filename, pne number, and package name at that point in your program.
They may be used only as separate tokens and will not be interpolated into strings. Check the below example −
#!/usr/bin/perl print "File name ". __FILE__ . " "; print "Line Number " . __LINE__ ." "; print "Package " . __PACKAGE__ ." "; # they can not be interpolated print "__FILE__ __LINE__ __PACKAGE__ ";
This will produce the following result −
File name hello.pl Line Number 4 Package main __FILE__ __LINE__ __PACKAGE__Advertisements