Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

how to properly handle the closing of pipe? read from the pipe unzip-c and get out before

by mishin (Sexton)
on Mar 29, 2011 at 14:05 UTC ( #896172=perlquestion: print w/ replies, xml ) Need Help??
mishin has asked for the wisdom of the Perl Monks concerning the following question:

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.
#!/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 -p $file_src $file" or croak "bah unzip -p $file_src $file"; $ret = search_trade_by_index( $FH, $out_file, $STRING_NUMBER2EXIT ); close $FH or croak "unable to close: unzip -p $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; }
with best regards, Mishin Nikolay, web-programmer

Comment on how to properly handle the closing of pipe? read from the pipe unzip-c and get out before
Download Code
Re: how to properly handle the closing of pipe? read from the pipe unzip-c and get out before
by Illuminatus (Curate) on Mar 29, 2011 at 15:03 UTC
    I'm sorry, but your english is hard to understand. Let me re-phrase, and you tell me if I have misinterpreted anything.

    You have a perl program that needs to extract some information from the beginning of a file in a .zip. As this script is on a production system, you cannot install additional modules that might make this easier. The script as written passes perlcritic, but dies on the croak line for the pipe close. Removing the croak statement (obviously) makes the error disappear, but then it won't pass perlcritic. You believe that the code as written results in a memory leak of some sort, apparent when a large (>10GB) is being unzipped. Does this sound correct?

    Given your source (thank you for formatting it properly), I have a couple of general comments:

    1. In your croak statement, you should be using $!, not $ERRNO. This should give you more info on why the close is failing
    2. Unfortunately, $! will only give you the error associated with the close. unzip uses its exit status to identify any reason it had for failure, but I don't think you can get this from the close
    3. You do realize that by using -c instead of -p, it will print the file name before it prints the file data? Your parsing routine does not seem to take this into account.

    fnord

      1. Thank you for translate, you understand correctly
      2. unzip -p it's really work !
      Mishin
      3. you advise to add. percritic
      [Variables::ProhibitPunctuationVars] allow = $@ $!
      mishin
        you advise to add. percritic

        I'd advise dumping perlcritic.

        Advising against using $! is like advising ignoring the temperature warning light on your dashboard and insisting you only regard spoken messages in English. (A language 2/3rds of the programmers in the world don't understand!).


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
      In your croak statement, you should be using $!, not $ERRNO.

      Why?  As he's using English, $ERRNO should be the same as $!.

        You are right, of course. I never use anything but $!, so I had forgotten this. The OP error output seemed to indicate that $ERRNO was blank. I was unsure as to why, or whether he simply omitted it. At any rate, it looks like his problem was fixed by changing the params to unzip

        fnord

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://896172]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-08-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls