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

comment on

( [id://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.

Thanks

Sam


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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found