http://qs321.pair.com?node_id=857875


in reply to Re^2: Printing to a File from Sub
in thread Printing to a File from Sub

Putting the filehandle in parens told Perl that it is the argument to print, and so you wanted to print it to STDOUT - which is not what you wanted. You wanted to say
print $fhout "this is a test\n";
It should also be noted that the variable $fhout is actually still in scope in your subroutine, so you technically could just use it. It is, however, better practice to pass it in.

Edit: choroba is correct as s/he notes below. I did mistake the curlies for parens.

Replies are listed 'Best First'.
Re^4: Printing to a File from Sub
by choroba (Cardinal) on Aug 30, 2010 at 16:50 UTC
    Not true. The problem is $fhout contains the number of elements in @_, not the filehandle. The syntax print {$fh} "out" is even recommended in PBP.
Re^4: Printing to a File from Sub
by suaveant (Parson) on Aug 30, 2010 at 19:33 UTC
    Yes, those aren't parens, they are squiggle brackets, easy mistake to make with the right font and font size.

                    - Ant
                    - Some of my best work - (1 2 3)