Running unix commands within Perl Scripts
Hi ,
I am using the Linux operating system and as well as using perl scripting for specific task.I am new for perl programming but much familiar with linux.I want to execute the Linux commands in in the perl script.How it would be done.i will be highly obliged to you.
Thank you very much
Running unix commands within Perl Scripts
You can execute the Linux commands within the perl script file and can be more beneficial for you to execute the commands within the program.
This is one of the program which is embedded with the Linux commands and will execute successful.
Code:
{
my $dir = './logs';
opendir my $dh, $dir or die "Can't opendir '$dir': $!\n";
my @files = grep { ! -d "$dir/$_" and ! /file1/ } readdir $dh;
print "$_\n" for @files;
}
exec() and system() function within Perl Scripts
exec() and system() function:
There are two functions are available in Perl named exec() function and system() that are being used to execute a system shell command.
The difference between these two functions are - system() creates a fork process and waits to see if the command succeeds or fails - returning a value.
On other hand,exec() doesn't evaluate anything after execution , it only executes the command.
Using backtick operator,you can capture output of the system call and it can't be used directly to get the capture output.
exec and system function with Perl Scripts
The system and exec function can be used during creation of script in perl.You can use these functions as below-
A simple example can show you the functionality and implementation of the functions which would be as follows -
Code:
$result = system(PROGRAM);
exec(PROGRAM);
The system function will manipulate the variable assigned in the parameter and the result variable is defined to get the output from this function.
Command-Line Programs in perl
Command-Line Programs in perl :
You need not use another editor and all to execute the programs in perl.You can easily run the small bit Perl programs on the command line.
You need to use some option which makes able you to execute the script.The -e option allows you to define Perl code to be executed by the compiler. For example, You can execute the "Hello William..." on the command line argument and it would print without any option.I am using it as a simple program to understanding for you.
Code:
$ perl -e 'print "Hello William...\n"'
You can use another -e option to print the output of the program.
Information about Perl components
You can get easily and readable information about the programming components of the perl.There are some commands with appropriate options will facilitate you to access the function and documentation about the perl programming -
To know more about the perl system function,just type the commands suggested below -
perldoc -f system
You can use the same method to access different components of the perl documentation.