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.
In reply to returning filehandles by DeusVult
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |