CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
how to properly handle the closing of pipe? read from the pipe unzip-c and get out before

 



mishin
Novice

Mar 29, 2011, 6:31 AM

Post #1 of 3 (2142 views)
how to properly handle the closing of pipe? read from the pipe unzip-c and get out before Can't Post

What makes the script: read zip length of the desired piece and comes from the cycle of reading early in order to save time
P.S. : Everything happens on the productive
$ Showrev
Release: 5.10
Kernel architecture: sun4u
Application architecture: sparc
Hardware provider: Sun_Microsystems
Kernel version: SunOS 5.10 Generic_142900-09

put additional perl modules I can not (like Archive:: Zip, IO:: Compress:: Zip)

There is code in perl, which is ideally passes through perlclitic on Brutal (report all violation), but
when the perl pipe_quest.pl
unable to close: unzip-c test.zip utp.live.stp.published.messages.txt at pipe_quest.pl line 29

creates the file you want, but an error,
how to handle this error correctly, that it was not?

if you close $ FH; do not handle, the code does not pass on perlcritic
Yes, and this may cause a memory leak if the file is large (> 10Gb) and close the pipe still need to be, controlling this leakage.

Code
#!/usr/bin/perl 
##############################################################################
# $URL: http://mishin.narod.ru $
# $Date: 2011-02-17 20:53:20 +0300 (Mon, 14 Feb 2011) $
# $Author: mishin nikolay $
# $Revision: 1.02 $
# $Source: pipe_quest.pl $
# $Description: script read data from zip file $
##############################################################################
use 5.006;
use strict;
use warnings;
use English qw(-no_match_vars);
use Carp;
our $VERSION = '0.01';
use Readonly;
Readonly my $STRING_NUMBER2EXIT => 1000;
my $EMPTY = q{};
my $ret = $EMPTY;
my $file_src = 'test.zip';
my $out_file = 'out.xml';
my $file = get_name_from_zip($file_src);
open my $FH, q{-|}, "unzip -c $file_src $file"
or croak "bah $file_src";
$ret = search_trade_by_index( $FH, $out_file, $STRING_NUMBER2EXIT );
close $FH or croak "unable to close: unzip -c $file_src $file $ERRNO";
sub search_trade_by_index {
my ( $fh, $xml_filename, $quit_str ) = @_;
my @xml_out = ();
my $xml_out = $EMPTY;
LINE: while ( my $line = <$fh> ) {
push @xml_out, $line;
if ( $INPUT_LINE_NUMBER eq $quit_str ) {
$xml_out = join $EMPTY, @xml_out;
$ret = file_write( $xml_filename, $xml_out );
last LINE;
}
}
return 1;
}
sub get_name_from_zip {
my ($loc_file_src) = @_;
my ( $ca, $cb, $cc, $loc_file );
my @result = readpipe "unzip -l $loc_file_src";
foreach (@result) {
if (/txt$/xsm) {
( $ca, $cb, $cc, $loc_file ) = split;
}
}
return $loc_file;
}
sub file_write {
my ( $wfile, $message ) = @_;
open my $fh, q{>}, "$wfile" or croak "unable to open:$wfile $ERRNO";
$ret = print {$fh} $message;
close $fh or croak "unable to close: $wfile $ERRNO";
return 1;
}



miller
User

Mar 29, 2011, 8:53 AM

Post #2 of 3 (2134 views)
Re: [mishin] how to properly handle the closing of pipe? read from the pipe unzip-c and get out before [In reply to] Can't Post

Looks like the mates over at perlmonks have this one

http://www.perlmonks.com/?node_id=896172


(This post was edited by miller on Mar 29, 2011, 8:54 AM)


mishin
Novice

Mar 29, 2011, 9:35 AM

Post #3 of 3 (2131 views)
Re: [miller] how to properly handle the closing of pipe? read from the pipe unzip-c and get out before [In reply to] Can't Post

ok it's mypost, then close it?

I have not yet decided its mission

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives