Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How can I pass File_Handle to a subroutine?

by Hanken (Acolyte)
on Jul 01, 2008 at 08:05 UTC ( #694901=perlquestion: print w/replies, xml ) Need Help??
Hanken has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks!
I got serval hash tables and I want to print them into separated files. So I write a sub and wanna pass a FILE_H to it. My code is:
open (FH_01 , ">", "file_01.txt" ) || die "open failed $!"; &hash2file(\%hash_regs, <FH_01>); # it dosen't work &hash2file(\%hash_regs, FH_01); # illegal syntax &hash2file(\%hash_regs, "file_01.txt"); # illegal syntax too
Can anybody tell me how to do it, or just give me some hints on how to quickly write different hashes into different files? Thanks!

Replies are listed 'Best First'.
Re: How can I pass File_Handle to a subroutine?
by psini (Deacon) on Jul 01, 2008 at 08:16 UTC

    Or try:

    open (my $FH_01 , ">", "file_01.txt" ) || die "open failed $!"; &hash2file(\%hash_regs, $FH_01); # it should work

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      Oh, thanks, it works. BTW: what is the difference between $FH_01 and <FH_01>? I feel the data structure of <FH_01> is very confusing.

        $FH_01 is a scalar variable that holds a filehandle, you can use it to access (read, write, stat) the file. <FH_01> is actually a call to the readline function on the FH_01 filehandle (not the same as the $FH_01 variable!).

        --
        David Serrano

Re: How can I pass File_Handle to a subroutine?
by Burak (Chaplain) on Jul 01, 2008 at 08:11 UTC
    Try it as \*FH_01

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://694901]
Approved by Corion
help
Chatterbox?
LanX looking for a good magit tutorial
LanX ... and git in general
[erix]: I've been to Amsterdam before you were born
[LanX]: 1899?
[chacham]: Just paint the white house black again.
dbander snorts
LanX Gesundheit! :)
talexb wonders about dividing developers in those born before the Unix timestamp of zero .. and after. Spoiler: I'm before.
chacham remebers I am a mad scientist

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-08-18 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (302 votes). Check out past polls.

    Notices?