Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Persistence of a filehandle for a large file with varying formats

by seaver (Pilgrim)
on Jan 25, 2013 at 09:09 UTC ( #1015291=perlquestion: print w/replies, xml ) Need Help??
seaver has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

This isn't going to be a very good question as it's 3am, but I'm struggling to make my code flexible as to whether it needs to open a plain text file, or a gzipped file. Thus I have in place this code:

sub getFileHandle{ my $file=shift; my $stream; if($file =~ /\.gz$/){ my $buffer=""; my $tmp_handle=""; gunzip $file => \$buffer or die "gunzip failed: $GunzipError\n +"; open $tmp_handle , "<", \$buffer or die "couldn't open stream\ +n"; $stream = (File::Stream->new($tmp_handle, separator => qr{[\cM +\r\n]}))[1]; }else{ open(FILE, "< $file") or die "\ncouldn't open FILE: ".$file.": + $! "; $stream = (File::Stream->new(\*FILE, separator => qr{[\cM\r\n] +}))[1]; } return $stream; }

However, my software package (if you can call it that) needs the filehandle itself to be persistent, in order for me to 'seek' back to the beginning multiple times. I'm not able to get my head around this given the two different ways in which I open a file, the normal way, and using IO::Uncompress::Gunzip, which really speaks to my inexperience with I/O (I copied the gunzip section from someplace).

So my question boils down to two things: 1) can I store a persistent filehandle for either a flat file or a gzipped file in the same variable in my package, if so, how? 2) Am I able to 'seek' within a filehandle the same way regardless of the source?

I haven't formulated this very well but will be happy to try and expand further.



Replies are listed 'Best First'.
Re: Persistence of a filehandle for a large file with varying formats
by pmqs (Pilgrim) on Jan 25, 2013 at 13:39 UTC
    IO::Uncompress::Gunzip gives you a filehandle interface to a gzip file
    my $fh = new IO::Uncompress::Gunzip "input file", or die "Cannot open file ..." ; my $line = <$fh> ;
    It also has the ability to assume the input file is uncompressed if the input data isn't a gzipped at all. To enable that mode just give it the Transparent option like this
    my $fh = new IO::Uncompress::Gunzip "input file", Transparent => 1 or die "Cannot open file ..." ; my $line = <$fh> ;

    That will give you a filehandle that can read from either a gzip compressed file or uncompressed input file.

    One limitation of IO::Uncompress::Gunzip is that it doesn't allow you to seek backwards in a file. If you have a requirement to be able to rewind the input file you will have to reopen the file again. Perhaps something like this (untested)

    sub rewind { $_[0]->close(); $_[0] = new new IO::Uncompress::Gunzip $_[1], Transparent => 1 or die "Cannot open file ..." ; } rewind($fh, "somefile");

      Well there you have it, a straight forward answer, I'll test this now. Thank you!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-08-15 14:31 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (161 votes). Check out past polls.