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

It seems like you've overlooked the most obvious solution:

open(my $messages,'>', 'msg.txt') or die "$!";

or, if you really mean to use the global property associated with bareword filehandles,

open(our $messages,'>', 'msg.txt') or die "$!";

Is there a reason you are not using lexicalindirect file handles? ( and using 2-argument instead of 3-argument open? )

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:12 UTC
    Is there a reason you are not using lexical file handles?

    Yes - the file will be opened on startup of the program, but the actual writing - if something is written at all - is performed at various other places (for instance by code pulled in from a file and evaluated with do. Actually, this is part of a debugging aid for our app, so there is no fancy solution needed; in contrary, it should be as simple as possible :-D.

    -- 
    Ronald Fischer <ynnor@mm.st>
      the actual writing - if something is written at all - is performed at various other places

      I'd encapsulate the logging in subroutine, rather than a variable.

      Yes - the file will be opened on startup of the program, but the actual writing - if something is written at all - is performed at various other places

      That doesn't answer the question since those "various other places" can call a function that has access to the lexical variable without having access to the lexical variable themselves.

      Less coupling == good.

      Sorry, I meant indirect, not lexical, and have updated the original post appropriately. If the only issue you wish to resolve is removing the warning, it's probably best to use

      BEGIN { no warnings qw(once); open(MESSAGES,'>msg.txt') or die "$!"; }

      since the warning removal will be scoped to the begin block and will only affect the known warning (see perllexwarn for the list of warning categories). Use of our will also suppress the warning and maintain global scope. Global scope can also be accomplished with package variables (open($main::messages,'>', 'msg.txt') or die "$!";), but this will still display the once warning, as well as will have precedence issues w/o the parentheses.