Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to store a filehandle in a hash of arrays ( $$LOG{$_}[0]=*FH; ) How to I do such a thing and how to I read lines from/open/close it?

Replies are listed 'Best First'.
Re: Storing filehandles in non-scalar datastructures
by merlyn (Sage) on Dec 03, 2000 at 03:32 UTC
    use FileHandle will permit you to call $handle->readline and $handle->print(@list). And there are other ways.

    -- Randal L. Schwartz, Perl hacker


    update: Sorry, that should be getline not readline.
        Okay, trying that method: $$LOG{$_}[0]=do {local *FH;}; both of these lines:
        print $$LOG{$_}[0]; print scalar <$$LOG{$_}[0]>;
        return:
        *main::FH
      Well calling readline ( ->readline() ) doesn't seem to work (Can't locate auto/FileHandle/readline.al in @INC). And niether does <$$LOG{$_}[0]>, but I think that's because I'm not using a scalar.
      Woo Hoo!! getline did it... thanks for all your help guys.
Re: Storing filehandles in non-scalar datastructures
by repson (Chaplain) on Dec 03, 2000 at 04:25 UTC
    Any element in an array or hash is a scalar. A scalar can be a reference to something such as another array or hash or a filehandle. Here is a WTDI.
    local *FH; # need this if in a loop or repeated sub open FH, 'blah' or die "can't open blah: $!\n"; $$LOG{$foo}[$blah] = \*FH; $in = readline($$LOG{$foo}[$blah]); # don't try <>, it doesn't like c +omplex structures print {$$LOG{$foo}[$blah]} $out; # must use a block {} for the fileha +ndle