Lots of things you could do differently, depending on where you want to go today!

If I read your example correctly, you print a whole lot of stuff into FILE and then more of the same stuff to either FILE or TEMPFILE? This is probably not what you really are trying to do?

FileHandle is a good way of working with filehandles 8-)

Subs are a good way of organising lots of repeated lines of code

You can pass parameters to subs to make them behave differently - for example:

use strict; use FileHandle; my $outFile = "tmp.txt"; my $outFile1 = "tmpo.txt"; my $fhOut = FileHandle->new(">$outFile") or die "Cannot open file $ou +tFile: $!"; my $fhTemp = FileHandle->new(">$outFile1") or die "Cannot open file $o +utFile1: $!"; my $test = "TRUE"; saveStuff( $fhOut ); if($test =~ /\bTRUE\b/){ saveStuff( $fhOut ); }elsif($test =~ /\bFALSE\b/){ saveStuff( $fhTemp ); } $fhOut->close(); $fhTemp->close(); sub saveStuff { my $fh = shift; my ( @things ) = qw( apples pears peaches cream ); # You can use join() to add tabs between all the things you # are printing rather than printf $fh->print( join("\t", @things ) . "\n"); #100 lines of similar code here... }

In reply to Re: how can i use one handle for multifple files by jaa
in thread how can i use one handle for multifple files by Anonymous Monk

Title:
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, details, 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, summary, 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.