Saturday, July 21, 2012

Tutorial on External System Calls from PERL or TCL/TK Program


Tutorial on External System Calls from PERL or TCL/TK Program

Tutorial on External System Calls from PERL or TCL/TK Program
There are several ways to call external system calls from inside the PERL or TCL/TK Program, we use many of these in several of our Integration Line Areas such as SCHAPI Software, Perl Programs etc.
1: Using System function : We use system function when we don’t want to capture the output of the command run, we can only get to know that whether the command succeeded or not.Ex: From our JOB where it just triggers the external program and doesn’t capture the output of PROGRAM.
system ‘ls -l $HOME’;
if ($RUN_TYPE eq “I” || $RUN_TYPE eq “S” ) {
if ( $RUN_TYPE eq “S” ){
system(“perl D:\\scmwrk82\\J2EEServer\\config\\CRM\\Poller_Libra\\ShutDownPoller.pl ‘E’”);
}
else {
system(“perl D:\\scmwrk82\\J2EEServer\\config\\CRM\\Poller_Libra\\ShutDownPoller.pl ‘I’”);
}
}
2: exec
It is same as system function except that it does not start child process, The exec function causes the Perl process itself to perform the requested action.Ex: From our Script to call our external script of Unix.
button .frame_bottom.ok \
-relief raised \
-text “Yes” -font $MediumFont -fg red \
-borderwidth 2 \
-command { exec /opt/gemini/prj/oper/bin/cleaning_maestro.sh &
exit }
3: backticks
We want to capture the output of the command,
Like : my $now = `date`; # grab the output of date
print “The time is now $now”;
Backquotes in list context , If the output from a command has multiple lines, the scalar use of backquotes returns it as a single long string containing newline characters. However, using the same backquoted string in a list context yields a list containing one line of output per element.
my $who_text = `who`; ( stores the output in singe long string)
my @who_lines = `who`; (stores the output in list context)
4: Using open function ( process as Filehandles)
When we want to pipe the command (as input or output) to the script.There are 2 ways to do it :
a : capture the data of a command (syntax: open(“command |”))
b: feed an external command with data generated from the Perl script (syntax: open(“| command”))
open DATE, “date|” or die “cannot pipe from date: $!”;
here we are trying to run the date command and piping its output to DATE file handle
now we can read the output : my $now = <DATE>;
open MAIL, “|mail merlyn” or die “cannot pipe to mail: $!”;
Here we are trying to send data to the mail process,
print MAIL “The time is now $now”; # presume $now ends in newline;
close MAIL;
die “mail: nonzero exit of $?” if $?;
Now one small question arises ,why do we use processes as filehandles? (when the same task can be done using backquotes ? , Well, it’s the only easy way to write to a process, based on the results of a computation. If you’re only reading, backquotes can be easier , to manage unless you want to have the results as they come in.Example :
open F, “find / -atime +90 -size +1000 -print|” or die “fork: $!”;
while (<F>) {
chomp;
printf “%s size %dK last accessed on %s\n”,
$_, (1023 + -s $_)/1024, -A $_;
}
The find command is looking for all the files not accessed within the past 90 days and larger than 1,000 blocks. While find is searching, Perl can wait. As each file is found, Perl responds to the incoming name and displays some information about that file for further research.Had this been written with backquotes, we wouldn’t have seen any output until the find command had finished. It’s comforting to see that it’s actually doing the job before it’s done.

No comments:

Post a Comment