Perl - Process Management
You can use Perl in
various ways to create new processes as per your requirements. This tutorial
will list down few important and most frequently used methods of creating and
managing Perl processes.
·
You can use special variables $$ or $PROCESS_ID to
get current process ID.
·
Every process created using any of the mentioned methods, maintains its
own virtual environment with-in %ENV variable.
·
The exit() function always exits just the child process
which executes this function and the main process as a whole will not exit
unless all running child-processes have exited.
·
All open handles are dup()-ed in child-processes, so that closing any
handles in one process does not affect the others.
Backstick Operator
This simplest way of
executing any Unix command is by using backstick operator. You simple put your
command inside backstick operator, which will result in execution of the
command and returns its result which can be stored as follows:
#!/usr/bin/perl
@files = `ls -l`;
foreach $file (@files){
print $file;
}
1;
When above code is executed,
it lists down all the files and directories available in the current directory:
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544
401 4096 Jul
6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy
The system() Function
You can also
use system() function to execute any Unix command, whose
output will go to the output of the perl script. By default, it is the screen,
i.e. STDOUT, but you can redirect it to any file by using redirection operator
>:
#!/usr/bin/perl
system(
"ls
-l")
1;
When above code is
executed, it lists down all the files and directories available in the current
directory:
drwxr-xr-x 3 root root 4096 Sep 14 06:46 9-14
drwxr-xr-x 4 root root 4096 Sep 13 07:54 android
-rw-r--r-- 1 root root 574 Sep 17 15:16 index.htm
drwxr-xr-x 3 544
401 4096 Jul
6 16:49 MIME-Lite-3.01
-rw-r--r-- 1 root root 71 Sep 17 15:16 test.pl
drwx------ 2 root root 4096 Sep 17 15:11 vAtrJdy
Be careful when your
command contains shell environmental variables like $PATH or $HOME. Try
following three scenarios:
#!/usr/bin/perl
$PATH = "I am Perl
Variable";
system('echo $PATH'); # Treats $PATH as shell variable
system("echo $PATH"); # Treats $PATH as Perl variable
system("echo \$PATH"); # Escaping $ works.
1;
When above code is
executed, it produces following result depending on what is set in shell
variable $PATH.
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
I am Perl
Variable
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
The fork() Function
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 undef if the fork is unsuccessful.
You can use exec() function
within a process to launch the requested executable, which will be executed in
a separate process area and exec() will wait for it to complete before exiting
with the same exit status as that process.
#!/usr/bin/perl
if(!defined($pid = fork())) {
# fork returned undef, so
unsuccessful
die "Cannot fork a child: $!";
}elsif ($pid == 0) {
print "Printed by child process\n";
exec("date") || die "can't exec
date: $!";
} else {
# fork returned 0 nor undef
# so this branch is parent
print "Printed by parent process\n";
$ret = waitpid($pid, 0);
print "Completed process id: $ret\n";
}
1;
When above code is
executed, it produces following result:
Printed by parent process
Printed by child process
Tue Sep 17 15:41:08 CDT 2013
Completed process id: 17777
The wait() and waitpid() can
be passed a pseudo-process ID returned by fork(). These calls will properly
wait for the termination of the pseudo-process and return its status. If you
fork without ever waiting on your children using waitpid() function,
you will accumulate zombies. On Unix systems, you can avoid this by setting
$SIG{CHLD} to "IGNORE" as follows:
#!/usr/bin/perl
local $SIG{CHLD} = "IGNORE";
if(!defined($pid = fork())) {
# fork returned undef, so
unsuccessful
die "Cannot fork a child: $!";
}elsif ($pid == 0) {
print "Printed by child process\n";
exec("date") || die "can't exec
date: $!";
} else {
# fork returned 0 nor undef
# so this branch is parent
print "Printed by parent process\n";
$ret = waitpid($pid, 0);
print "Completed process id: $ret\n";
}
1;
When above code is
executed, it produces following result:
Printed by parent process
Printed by child process
Tue Sep 17 15:44:07 CDT 2013
Completed process id: -1
The kill() Function
Perl kill('KILL',
(Process List)) function can be used to terminate a pseudo-process by
passing it the ID returned by fork().
Note that using
kill('KILL', (Process List)) on a pseudo-process() may typically cause memory
leaks, because the thread that implements the pseudo-process does not get a
chance to clean up its resources.
You can use kill() function
to send any other signal to target processes, for example following will send
SIGINT to a process IDs 104 and 102:
#!/usr/bin/perl
kill('INT',
104,
102);
1;
Perl - Embedded
Documentation
You can embed Pod
(Plain Old Text) documentation in your Perl modules and scripts. Following is
the rule to use embedded documentation in your Perl Code:
Start your documentation with an empty line, a =head1 command
at the beginning, and end it with a =cut command and an empty
line.
Perl will ignore the
Pod text you entered in the code. Following is a simple example of using
embedded documentation inside your Perl code:
#!/usr/bin/perl
print "Hello, World\n";
=head1 Hello, World Example
This example demonstrate very basic syntax of Perl.
=cut
print "Hello, Universe\n";
When above code is
executed, it produces following result:
Hello, World
Hello, Universe
If you're going to
put your Pod at the end of the file, and you're using an __END__ or __DATA__
cut mark, make sure to put an empty line there before the first Pod command as
follows, otherwise without an empty line before the =head1, many
translators wouldn't have recognized the =head1 as starting a
Pod block.
#!/usr/bin/perl
print "Hello, World\n";
while(<DATA>){
print $_;
}
__END__
=head1 Hello, World Example
This example demonstrate very basic syntax of Perl.
print "Hello, Universe\n";
When above code is
executed, it produces following result:
Hello, World
=head1 Hello, World Example
This example demonstrate very basic syntax of Perl.
print "Hello, Universe\n";
Let's take one more
example for the same code without reading DATA part:
#!/usr/bin/perl
print "Hello, World\n";
__END__
=head1 Hello, World Example
This example demonstrate very basic syntax of Perl.
print "Hello, Universe\n";
When above code is
executed, it produces following result:
Hello, World
What is POD?
Pod is a
simple-to-use markup language used for writing documentation for Perl, Perl
programs, and Perl modules. There are various translators available for
converting Pod to various formats like plain text, HTML, man pages, and more.
Pod markup consists of three basic kinds of paragraphs:
·
Ordinary Paragraph: You can use formatting
codes in ordinary paragraphs, for bold, italic, code-style , hyperlinks, and
more.
·
Verbatim Paragraph: Verbatim paragraphs
are usually used for presenting a codeblock or other text which does not
require any special parsing or formatting, and which shouldn't be wrapped.
·
Command Paragraph: A command paragraph is used for
special treatment of whole chunks of text, usually as headings or parts of
lists. All command paragraphs start with =, followed by an
identifier, followed by arbitrary text that the command can use however it
pleases. Currently recognized commands are:
=pod
=head1 Heading Text
=head2 Heading Text
=head3 Heading Text
=head4 Heading Text
=over indentlevel
=item stuff
=back
=begin format
=end format
=for format text...
=encoding type
=cut
POD Examples
Consider the
following POD:
=head1 SYNOPSIS
Copyright 2005 [TUTORIALSOPOINT].
=cut
You can use pod2html utility
available on Linux to convert above POD into HTML, so it will produce following
result:
Copyright 2005 [TUTORIALSOPOINT].
Next consider the
following example:
=head2 An Example List
=over 4
=item * This is a bulleted list.
=item * Here's another item.
=back
=begin html
<p>
Here's some embedded HTML. In this block I can
include images, apply <span style="color: green">
styles</span>, or do anything else I can do with
HTML. pod parsers that aren't outputting HTML will
completely ignore it.
</p>
=end html
When you convert
above POD into HTML using pod2html, it will produces following result:
An Example List
- This is a bulleted list.
- Here's another item.
Here's some embedded
HTML. In this block I can include images, apply styles, or do anything else
I can do with HTML. pod parsers that aren't outputting HTML will completely
ignore it.
Comments
Post a Comment