hello Marcool

you got very good advices and, more important, it seems you understandood them.

I want just a more general though: read and learn from idiomatic perl (a section of the worth to read modern perl book)

Also rethink your code: Perl offers you many many way to do the job, but why you want to print different lines to a file clobbering it each time?

It is not better to choice the line worth to store, and only after print it to the file?

if you extract the choice logic you can have a sub instead of not so idiomatic (but correct and valid) do {} untill $condition; part.

Subs can be declared also after their usage in the code, and the program become more readable (more if you choose good names! mysub is not a good one..)

Consider for example

print $fh choose_content(); sub choose_content{ my $condition = 0; my $returned=''; while($condition == 0){ $returned = "Line to be printed\n"; #... other stuff goes on, eventually changing $condition to 1 # but probably changing $returned too! } return $returned; }

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Writing to file with filehandler in variable, scope issue, clobber becomes append by Discipulus
in thread Writing to file with filehandler in variable, scope issue, clobber becomes append by Marcool

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.