Mohd Mohtashim

Mohd Mohtashim

185 Articles Published

Articles by Mohd Mohtashim

Page 3 of 19

Grouping Matching in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ Views

From a regular-expression point of view in Perl, there is no difference between the following two expressions except that the former is slightly clearer.$string =~ /(\S+)\s+(\S+)/; and $string =~ /\S+\s+\S+/;However, the benefit of grouping is that it allows us to extract a sequence from a regular expression. Groupings are returned as a list in the order in which they appear in the original. For example, in the following fragment we have pulled out the hours, minutes, and seconds from a string.my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);As well as this direct method, matched groups are also available within the ...

Read More

The G Assertion in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 397 Views

The \G assertion in Perl allows you to continue searching from the point where the last match occurred. For example, in the following code, we have used \G so that we can search to the correct position and then extract some information, without having to create a more complex, single regular expression −Example#!/usr/bin/perl $string = "The time is: 12:31:02 on 4/12/00"; $string =~ /:\s+/g; ($time) = ($string =~ /\G(\d+:\d+:\d+)/); $string =~ /.+\s+/g; ($date) = ($string =~ m{\G(\d+/\d+/\d+)}); print "Time: $time, Date: $date";When the above program is executed, it produces the following result −Time: 12:31:02, Date: 4/12/00The \G assertion is actually ...

Read More

What are Packages in Perl?

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 416 Views

The package statement in Perl switches the current naming context to a specified namespace (symbol table). Thus −A package is a collection of code which lives in its own namespace.A namespace is a named collection of unique variable names (also called a symbol table).Namespaces prevent variable name collisions between packages.Packages enable the construction of modules which, when used, won't clobber variables and functions outside of the modules's own namespace.The package stays in effect until either another package statement is invoked, or until the end of the current block or file.You can explicitly refer to variables within a package using the :: package ...

Read More

BEGIN and END Blocks in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 2K+ Views

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 More

The fork() Function in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 5K+ Views

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 More

Hello World using Perl.

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 1K+ Views

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 More

Perl First Program

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 437 Views

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 More

Comments in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 5K+ Views

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 More

Whitespaces in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 992 Views

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 More

Single and Double Quotes in Perl

Mohd Mohtashim
Mohd Mohtashim
Updated on 11-Mar-2026 820 Views

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
Showing 21–30 of 185 articles
« Prev 1 2 3 4 5 19 Next »
Advertisements