in reply to Writing to file with filehandler in variable, scope issue, clobber becomes append

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.