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

Scalars in Perl

Last Updated: May 15, 2017 by Chaitanya Singh | Filed Under: Perl

Scalar data in Perl is considered as singular, it can either be number or string. We have covered a little bit about scalars in variables and data types tutorials. In this tutorial we will discuss few important things about strings, we would skip numbers as use of them are pretty straight forward.

Single quoted string vs double quoted string

Quotes are not part of the string, they just specify the beginning and the ending of the string. In Perl, single and double quotes behaves differently. To understand the difference, lets have a look at the code below:

#!/usr/bin/perl

print "Welcome to\t Beginnersbook.com\n";
print 'Welcome to\t Beginnersbook.com\n';

This would produce the following output:
Welcome to	 Beginnersbook.com
Welcome to\t Beginnersbook.com\n

You can clearly see the difference that double quotes interpolated escape sequences “\t” and “\n” however single quotes did not.

Another example:

#!/usr/bin/perl

$website = "Beginnersbook";
print "Website Name: $website\n";
print 'Website Name: $website\n';

Output:

Website Name: Beginnersbook
Website Name: $website\n

This is another advantage of double quotes as they are “variable interpolated”. Which means that variable name inside double quotes are replaced with their values. This doesn’t happen in single quoted string.

Use of Single quotes

You may be thinking of avoiding single quotes in perl program, however there are certain cases where you would want to use single quotes.
For e.g. If you want to store email address in a variable, in that case double quotes would throw an error, you need to use single quote in that case. For e.g.

$email = "[email protected]"; would throw an error 

$email = '[email protected]'; would work fine.

Backslash in Perl

The backslash can perform one of two tasks:
1) it takes away the special meaning of the character following it. For e.g. print “\$myvar”; would produce $myvar output instead of variable myvar value because the backslash(\) just before the $ takes away the special meaning of $
2) It is the start of a backslash or escape sequence. For e.g. \n is an escape sequence used for newline

what is the use of it?

Believe me, you would use it frequently while developing an application in perl. Lets say you want to print the string that contains double quotes. For e.g. If i want to print: Hello This is “My blog” then I would need to use following logic:

#!/usr/bin/perl

$msg = "Hello This is \"My blog\"";
print "$msg\n";

Output:

Hello This is "My blog"

String Operations

Concatenation:

You can concatenate strings using dot( . ) operator. For example:

"Hello"."Chaitanya" # Equivalent to "HelloChaitanya"
"Hello"."Chaitanya"."\n" # Equivalent to "HelloChaitanya\n"

String repetition:

Syntax: MyString x MyNumber
This would repeat the string “MyString” by “MyNumber” times.

Here x should be in lowercase.

For example:

"book" x 4  # Same as "bookbookbookbook"
"Tom" x (1+2) # Same as "Tom" x 3 or "TomTomTom"
"Wow" x 2.7 # Same as "Wow" x 2 or "WowWow"

In the third example 2.7 is considered 2, no number round off happens here.

Note: Perl considers the part before x operator as string and part after x is treated as number. In other words you can say that the left operand is treated as string and right operand as number.
For example:

8 x 3 # Same as "8" x 3 or "888"

You must remember this point while using x operator to avoid any confusion. 8 x 3 is not 24 instead it is “888”.

❮ PreviousNext ❯

Top Related Articles:

  1. First Perl Program
  2. Use strict and use warnings in Perl
  3. Perl Variables
  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