Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!sun-barr!newstop!male!texsun!convex!newsadm From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: filename from FILEHANDLE? Message-ID: <1991May01.034332.19057@convex.com> Date: 1 May 91 03:43:32 GMT References: Sender: newsadm@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Distribution: comp Organization: CONVEX Software Development, Richardson, TX Lines: 44 Nntp-Posting-Host: pixel.convex.com From the keyboard of dnb@meshugge.media.mit.edu (David N. Blank): : Is there a way to determine the file name which an open FILEHANDLE :represents? In my case I was wondering if given: : :open(FRED,"bedrock"); :&process(FRED); : :sub process { : ... :} : :could I determine in &process the actual name of the file I would be :working with? Thanks. Not in general, without at least some cooperation from the calling process: sub fdpath { local(*FH) = @_; $name{*FH}; } open(FRED, $name{*FRED} = "/etc/motd"); # all these should (or shouldn't :-) work... print "FRED is opened to ", &fdpath( FRED ), "\n"; print "FRED is opened to ", &fdpath('FRED'), "\n"; print "FRED is opened to ", &fdpath(*FRED ), "\n"; On a Convex, such cooperation is unnecessary -- there's a syscall for it: sub fdpath { local(*FH) = @_; local($path, $pathlen); $SYS_fdpath = 227; # from syscall.h $MAXPATHLEN = 1024; # from sys/param.h $pathlen = pack('L', length($path = "\0" x $MAXPATHLEN)); warn "fdpath: $!" if syscall($SYS_fdpath, fileno(FH), $path, $pathlen); substr($path, unpack('L', $pathlen)) = ''; $path; } Nifty, eh? It'll even give you the full pathname and works on FIFOs, too. --tom