Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create file handle from within sub in main scope

Status
Not open for further replies.

TheDauntless

Programmer
Feb 26, 2008
14
BE
Hi,

I'm wondering how I can create a file handle from within a sub routine, that is available in the entire document.

In my routine, there is, fe:
open (MYFILE,"> /test/output.txt");

How can I declare the (empty) scalar/variable in my main scope, so that I can 'fill it' from within my subroutine?

Greets!
 
Filehandles like that *are* global.

I'm not sure what you mean by your last sentence: can you elaborate?
 
Example:

Code:
sub mySub
{

open(MYFILE, "> path");
}

Is <MYFILE> accessible from everywhere in my code now? (Without having declared MYFILE outside the subroutine)
 
Have you tried it? That's the best way to find out without having to wait for people on a forum to tell you :)

Assuming you're not using multiple packages then yes, you can refer to that filehandle just with "MYFILE" anywhere (once you've called your subroutine of course). You'll have to refer to it differently if you're using different packages.
 
Funny, that's something I often tell other people (on a different forum :p)

It's just that scope handling in Perl is quite confusing, but thanks for your answer, it's a bit clearer to me now ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top