Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^3: Printing to a File from Sub

by pemungkah (Priest)
on Aug 29, 2010 at 00:50 UTC ( #857875=note: print w/replies, xml ) Need Help??

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)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://857875]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2023-11-28 23:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found