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

Perl Syntax

By Chaitanya Singh | Filed Under: Perl

We got familiar with the basics of Perl in our last tutorial: first perl program. In this tutorial, we will learn few important points regarding Perl syntax and naming convention.

Perl file naming convention

Perl file name can contain numbers, symbols, letters and underscore (_), however spaces are not allowed in the file name. for e.g. hello_world.pl is a valid file name but hello world.pl is an invalid file name.

Perl file can be saved with .pl or .PL extension.

Difference between single and double quotes in Perl

Single and double quotes behaves different in Perl program. To understand the difference, lets have a look 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

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"
❮ PreviousNext ❯

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 – 2022 BeginnersBook . Privacy Policy . Sitemap