DeusVult has asked for the wisdom of the Perl Monks concerning the following question:
I know (or believe I know) several things
My problem is that I want to open a file inside a subroutine, but have the resulting filehandle be available for use everywhere after the function is called. For example...
#example 1 HANDLE = &callafunction(); # what is HANDLE? I don't know print HANDLE "This prints to file.txt\n"; sub callafunction { open ( OUT, "file.txt" ) or die ( "open no worky!: $!" ); # somehow "return" OUT so it gets aliased to HANDLE }
Also, I may need to open several files in this subroutine, so I would like to be able to put these "file-handle-variables" into an array of some kind, and then return that like this...
#example 2 ( HANDLE1, HANDLE2 ) = &callafunction(); print HANDLE1 "This goes to file1.txt\n"; print HANDLE2 "This goes to file2.txt\n"; sub callafunction { open HANDLE1, "file1.txt" or die ( "open no worky!: $!" ); open HANDLE2, "file2.txt" or die ( "open no worky!: $!" ); my @array; # somehow push HANDLE1 and HANLDE2 onto @array @array; }
I used two examples because it occurred to me that it might be possible to do one of these tasks, but not the other (or someone might know the answer to one, and not the other).
Does anyone know how I can go about these tasks, if either of them are possible?
Thanks in advance.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: returning filehandles
by chipmunk (Parson) on Jan 31, 2001 at 00:48 UTC | |
Re (tilly) 1: returning filehandles
by tilly (Archbishop) on Jan 31, 2001 at 01:35 UTC | |
Re: returning filehandles
by Fastolfe (Vicar) on Jan 31, 2001 at 00:48 UTC | |
Re: returning filehandles
by DeusVult (Scribe) on Jan 31, 2001 at 01:25 UTC | |
by chipmunk (Parson) on Jan 31, 2001 at 02:06 UTC | |
by arturo (Vicar) on Jan 31, 2001 at 02:05 UTC |