Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.



In reply to Persistence of a filehandle for a large file with varying formats by seaver

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (6)
    As of 2018-04-25 11:52 GMT
    Find Nodes?
      Voting Booth?