in reply to Is it possible to "use vars" or "our" a file handle?

Try:

my $fileHandle; #Closes for you when it goes out of scope! open $fileHandle, '>', $fileName or die "Can't open $fileName: $!\n"; writeFancyLogging($fileHandle, "Loggin' some stuff to a file"); sub writeFancyLogging { my $file = shift; my $msg = shift; print $file $msg . "\n"; }

3-arg open is nicer and safer, using the variable twice solves the warning, and by my'ing it, you can pass it around and have it go out of scope.

Replies are listed 'Best First'.
Re^2: Is it possible to "use vars" or "our" a file handle?
by rovf (Priest) on Nov 02, 2009 at 16:04 UTC
    my $fileHandle; #Closes for you when it goes out of scope! open $fileHandle, '>', $fileName or die "Can't open $fileName: $!\n";
    Good suggestion (though to parallel my original one, I would have to use our $fileHandle instead of my). Actually, although it does not directly answer my original question (where I was looking for a solution using a global file handle, i.e. one written without a sigil), I think it complements nicely my "list of alternative solutions" which I had compiled in my posting.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re^2: Is it possible to "use vars" or "our" a file handle?
by rovf (Priest) on Nov 02, 2009 at 16:07 UTC
    3-arg open is nicer and safer
    In general yes. In this case it doesn't make a difference, since the filename is passed literally.

    -- 
    Ronald Fischer <ynnor@mm.st>