in reply to Problems writing to filehandles contained in variables

You can't store a glob into a variable and then use that variable. You can store a reference to a glob into a variable. If you replace
my @incHandles = (*INCNT, *INCMAIL, *INCUNIX);
with
my @incHandles = (\*INCNT, \*INCMAIL, \*INCUNIX);
it'll start to work. But the proper answer is to use IO::File objects, to keep you from even having to mention names for things that are never again referred as names:
my @incHandles; use IO::File; push @incHandles, IO::File->new for 1..3;
And if you're using a fairly recent version of Perl, you can even skip those last two steps. Simply using an undef var as if it was a filehandle autovivs as a filehandle! Cool!

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
(tye)Re: Problems writing to filehandles contained in variables
by tye (Sage) on Aug 11, 2002 at 00:59 UTC

    Actually, you can. For example, the following works just fine:

    my $stdout= *STDOUT; print $stdout "Hello\n";
    What you can't do is use a non-string as a hash key and expected it to stay a non-string.

    Even if the above code were switched to use \*INCNT (which I would recommend be done if for no other reason than so that it is more obvious that these are special things that aren't just strings) all that would happen would be that the error message would change to: Can't use string ("GLOB(0xbadd0g)") as a symbol ref while "strict refs" in use To solve the problem you could use Tie::RefHash or you could make the key just "INCNT" and have a value in the hash that is the handle:     $anchors{INCNT}{handle}= \*INCNT; (or an IO::Handle if you prefer).

            - tye (but my friends call me "Tye")