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

I'm having a bit of trouble with tracking my filehandles and wonder if anyone might have some suggestions.

I'm working on a system that tails a number of web logs and I need to be able to associate each of the filehandles with a domain name so I can know the domain when I get a line of input. Therefore, I need some way to map the filehandle back to the domain name.

All the handles are implemented as IO::File objects. I tried stashing the domain name in there but it appears that it is implemented as a typeglob so I can't hide anything in there.

At the moment, I've got the thing rigged with a hash with the IO::File objects as keys and the domain names as values. It is my understanding that, in this usage, the filehandle would be reduced to a scalar before being turned into a key. This strikes me as a very bad way to do things but I'm at a total loss as to what to do. Any ideas?

An 8 bit man in a 32 bit world.

Replies are listed 'Best First'.
Re: Storing metadata with file handles
by blakem (Monsignor) on Oct 04, 2002 at 18:43 UTC
    Perhaps you should use the file descriptor number as a lookup key:
    my %domain; my $filenum = fileno($fh); $domain{$filenum} = 'www.yoursite.com';

    -Blake

      That's perfect. Exactly what I was looking for.

      An 8 bit man in a 32 bit world.

        And if you want to get really evil (read nonportable, and somewhat unreliable) you can sometimes associate the filenum back to a filename. Warning: this code is fragile, won't always work, and will only work on unix.
        my $fileno = fileno($fh); my $filename = readlink("/dev/fd/" . $fileno);
        See: Re: Re: Can you delete a file by descriptor?

        -Blake

Re: Storing metadata with file handles
by kabel (Chaplain) on Oct 04, 2002 at 18:39 UTC
    the IO::File object is merely a reference, so you could use the Tie::RefHash module as replacement for the "ordinary" hash. that should mild down your headache some bits ;)