perlquestion
seaver
<p>Dear all,</p>
<p>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:
<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;
}
</code>
<p>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).</p>
<p>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?</p>
<p>I haven't formulated this very well but will be happy to try and expand further.</p>
<p>Thanks</p>
<p>Sam</p>