home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
- From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
- Subject: Re: array of file descriptors
- Message-ID: <mcook.724624901@fendahl.dev.cdx.mot.com>
- Sender: news@merlin.dev.cdx.mot.com (USENET News System)
- Nntp-Posting-Host: fendahl.dev.cdx.mot.com
- Organization: Motorola Codex, Canton, Massachusetts
- References: <1992Dec17.171242.22639@ans.net>
- Date: Thu, 17 Dec 1992 20:41:41 GMT
- Lines: 28
-
- fuat@news.ans.net (Fuat C. Baran) writes:
-
- >Is there a way to store file descriptors (from an open()) in an array?
- >I would like to open a set of files, store the open file descriptors
- >in an associative array keyed by filename and be able to multiplex
- >output to one of the files in a loop where for each line of output I'm
- >given a filename. I'd like to check if $filehandles{$filename} is
- >defined and if not first open the file.
-
- You can generate your own file handle symbols:
-
- $gensym = "fh0000";
- foreach $file (@file)
- {
- open(++$gensym, "<$file") || die "oops: $file, $!";
- $fh{$file} = $gensym;
- }
-
- The trick is using the file handle with the <> operator:
-
- $_ = <$fh{$file}>; # doesn't seem to work
-
- $tmp = $fh{$file}; # need an extra variable
- $_ = <$tmp>;
-
- See also cacheout.pl.
-
- Michael.
-