Perl system function - what it is?
Perl System:
system command invokes shell execution by default inside the brackets (). incorrect meta characters may lead errors in shell execution. However we have methods to byepass the shell execution without errors. using system we can able to pass more than one arguments as list for the execution.
we can perform most command line execution using system command.system perform the commands to execute and it waits for the completion of execution and then it returns the output to perl.
Single Argument System Commands:
it interacts with direct shell execution
Method 1:
Multiple Argument System Commands:
1.it byepass the shell execution and it avoids the meta characters interpretations and exploit Errors,
2.it is secured and less open security issues.
3.in this execution, first argument is commands remaing all are considered as arguments.
Method 1:
Method 2:[Perl built-in glob function]
Method 3
Error Tracking With $?:
$? is the special variable used for capture the system output. this output will be as like below
1. -1
2. 0
3. anything else
-1 will indicate the process is unable start due to some problem 0 indicate command was successful
If $? is anythingelse, we have to do number of bit-masking and bit-shifting operations to extract values
Method 1:
Method 2:
Method 3:
Method 4:
we can specify return values as first argument using IPC::System::Simple module.
1.returns 0, 1 ,2:
system( [0,1,2], "command");
2.captures exit value:
my $exit = system( [0,1,2], ’command’);
3.range operator:
my $exit = system( [0..2], ’command’);
system command invokes shell execution by default inside the brackets (). incorrect meta characters may lead errors in shell execution. However we have methods to byepass the shell execution without errors. using system we can able to pass more than one arguments as list for the execution.
we can perform most command line execution using system command.system perform the commands to execute and it waits for the completion of execution and then it returns the output to perl.
Single Argument System Commands:
it interacts with direct shell execution
Method 1:
#prints the string on shell system("echo Hello Perl"); # Get errors in syslog system("tail /var/fslog | grep -i ERROR"); # open notepad to edit a file system("notepad fslog.txt");
Multiple Argument System Commands:
1.it byepass the shell execution and it avoids the meta characters interpretations and exploit Errors,
2.it is secured and less open security issues.
3.in this execution, first argument is commands remaing all are considered as arguments.
Method 1:
system('ls', '*.txt');
Method 2:[Perl built-in glob function]
system('ls', glob(’*.txt’) );
Method 3
system('ls', @files);
Error Tracking With $?:
$? is the special variable used for capture the system output. this output will be as like below
1. -1
2. 0
3. anything else
-1 will indicate the process is unable start due to some problem 0 indicate command was successful
If $? is anythingelse, we have to do number of bit-masking and bit-shifting operations to extract values
Method 1:
system("commands"); if ($? == -1) { print "Cant run $!\n"; } elsif ($? == 0) { print "successfull\n"; } else { print "Exit number is ", $? >> 8, "\n"; print "Signal value is ", $? & 127, "\n"; print "Dumped value\n" if $? & 128; }
Method 2:
use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG); system("command");
if (WIFEXITED($?)) { print "normal exit value ", WEXITSTATUS($?),"\n"; } elsif (WIFSIGNALED($?)) { print "Killed the execution ",WTERMSIG($?),"\n"; } else { warn "it is not normal exit.\n";
}
Method 3:
eval { system(’backup_files’); system(’delete_files’); }; if ($@) { print "Error commands: $@\n"; }
Method 4:
eval { use autodie qw(system); system('touch a.txt'); system('rm a.txt'); }; if ($@) { print "Errors : $@\n"; }
we can specify return values as first argument using IPC::System::Simple module.
1.returns 0, 1 ,2:
system( [0,1,2], "command");
2.captures exit value:
my $exit = system( [0,1,2], ’command’);
3.range operator:
my $exit = system( [0..2], ’command’);
Labels: system, system function
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home