Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Mohd Mohtashim
Page 7 of 19
BEGIN and END Blocks in Perl
You may define any number of code blocks named BEGIN and END in Perl programs, which act as constructors and destructors respectively.BEGIN { ... } END { ... } BEGIN { ... } END { ... }Every BEGIN block is executed after the perl script is loaded and compiled but before any other statement is executed.Every END block is executed just before the perl interpreter exits.The BEGIN and END blocks are particularly useful when creating Perl modules.Following example shows its usage −Example#!/usr/bin/perl package Foo; print "Begin and Block Demo"; BEGIN { print "This is BEGIN Block" } END { ...
Read MoreThe fork() Function in Perl
Perl provides a fork() function that corresponds to the Unix system call of the same name. On most Unix-like platforms where the fork() system call is available, Perl's fork() simply calls it. On some platforms such as Windows where the fork() system call is not available, Perl can be built to emulate fork() at the interpreter level.The fork() function is used to clone a current process. This call create a new process running the same program at the same point. It returns the child pid to the parent process, 0 to the child process, or under if the fork is ...
Read MoreHello World using Perl.
Perl is a programming language developed by Larry Wall, specially designed for text processing.Just to give you a little excitement about Perl, I'm going to give you a small conventional Perl Hello World program,You can try it using the Demo link.Example#!/usr/bin/perl # This will print "Hello, World" print "Hello, world";
Read MorePerl First Program
Interactive Mode ProgrammingYou can use Perl interpreter with -e option at the command line, which lets you execute Perl statements from the command line. Let's try something at $ prompt as follows −$perl -e 'print "Hello World"'This execution will produce the following result −Hello, worldScript Mode ProgrammingAssuming you are already on $ prompt, let's open a text file hello.pl using vi or vim editor and put the following lines inside your file.Example#!/usr/bin/perl # This will print "Hello, World" print "Hello, world";Here /usr/bin/perl is actual the Perl interpreter binary. Before you execute your script, be sure to change the mode of ...
Read MoreComments in Perl
Comments in any programming language are friends of developers. Comments can be used to make program user-friendly and they are simply skipped by the interpreter without impacting the core functionality. For example, in the above program, a line starting with hash # is a comment.Simply saying comments in Perl start with a hash symbol and run to the end of the line −# This is a comment in perlLines starting with = are interpreted as the start of a section of embedded documentation (pod), and all subsequent lines until the next =cut are ignored by the compiler. Following is the ...
Read MoreWhitespaces in Perl
A Perl program does not care about whitespaces. Following program works perfectly fine −#!/usr/bin/perl print "Hello, world";But if spaces are inside the quoted strings, then they would be printed as is. For example −Example#!/usr/bin/perl # This would print with a line break in the middle print "Hello world";OutputThis will produce the following result −Hello worldAll types of whitespace like spaces, tabs, newlines, etc. are equivalent to the interpreter when they are used outside of the quotes. A line containing only whitespace, possibly with a comment, is known as a blank line, and ...
Read MoreSingle and Double Quotes in Perl
You can use double quotes or single quotes around literal strings as follows −Example#!/usr/bin/perl print "Hello, world"; print 'Hello, world';OutputThis will produce the following result −Hello, world Hello, world$There is an important difference between single and double-quotes. Only double quotes interpolate variables and special characters such as newlines , whereas a single quote does not interpolate any variable or special character. Check below example where we are using $a as a variable to store a value and later printing that value −Example#!/usr/bin/perl $a = 10; print "Value of a = $a"; print 'Value of a = $a';OutputThis will produce the ...
Read More"Here" Documents in Perl
You can store or print multiline text with great comfort. Even you can make use of variables inside the "here" document. Below is a simple syntax, check carefully there must be no space between the
Read MoreEscaping Characters in Perl
Perl uses the backslash (\) character to escape any type of character that might interfere with our code. Let's take one example where we want to print double quote and $ sign −Example#!/usr/bin/perl $result = "This is "number""; print "$result"; print "\$result";OutputThis will produce the following result −This is "number" $result
Read MoreWhat are Perl String Literals?
Strings are sequences of characters. They are usually alphanumeric values delimited by either single (') or double (") quotes. They work much like UNIX shell quotes where you can use single-quoted strings and double-quoted strings.Double-quoted string literals allow variable interpolation, and single-quoted strings are not. There are certain characters when they are proceeded by a backslash, have special meaning and they are used to represent like newline () or tab (\t).You can embed newlines or any of the following Escape sequences directly in your double-quoted strings −Escape sequenceMeaning\Backslash'Single quote"Double quote\aAlert or bell\bBackspace\fForm feedNewline\rCarriage return\tHorizontal tab\vVertical tab\0nnCreates Octal formatted numbers\xnnCreates Hexideciamal ...
Read More