Sunday 1 December 2019

perl piped open - reading data from a system process?

Perl Piped Open:

open function is very useful for reading or writing the data files on the system and apart from this file processing we can use pipe symbol for getting data from a system process or sending the data to a system process can be achieved.   lets try this now,




Method 1: [Calling process command by using Shell]


open (my $SSH, "ssh $hostname cat $filename |") or die "Can’t open pipe: $!";

while(<$SSH>) {
print;
}
close $SSH or die "Unable to close: $! -- $?";

Method 2:[using More than one argument]

open (my $SSH, "-|", "ssh", "$hostname", "cat", "$filename") or die "Can’t open pipe: $!";

while(<$SSH>) {
print;
}
close $SSH or die "Unable to close: $! -- $?";


NOTE:

1.If we open a process for file writing, it is necessary to setup handler to SIGPIPE.
2.we are setting the file handler for catching errors of closed process or undefined process .
3. %SIG is used for handing the errors,


my $SIG{PIPE} = sub { die "Pipe is cosed." };

open(my $output, "| $processcommand") or die $!;
print {$output} "Sample text";
close $output or die "Unable to close: $! -- $?";


kaavannan perl blogspot

Labels: , , , , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home