Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Difference between File Handles and File Descriptors in function parameters.

by tusty (Novice)
on Aug 11, 2011 at 14:16 UTC ( #919882=perlquestion: print w/ replies, xml ) Need Help??
tusty has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, A smart cookie recommended that a function I wrote should take file descriptors as arguments instead of file handles. What be the difference between the two?? The function is something on these lines. What advantage would using a file descriptor have on such a function?
sub printfile() { my ($fh) = @_; print <$fh>; }

Comment on Difference between File Handles and File Descriptors in function parameters.
Download Code
Re: Difference between File Handles and File Descriptors in function parameters.
by Skeeve (Vicar) on Aug 11, 2011 at 14:45 UTC

    If I read this Perl Cookbook Chapter 7 I don't see any advantage in using filedescriptors.

    Perl's I/O system uses filehandles instead of file descriptors, so you have to make a new filehandle for an already open file descriptor.


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
      thanks for your response, So what exactly is a file descriptor?? somehow I find that no page I found really answers that question, they just talk about how to use it. Why would any one want to pass or return a file descriptor?

        A file descriptor is a number which specifies a IO stream. In Unix and other POSIX environments, there is an array called the file descriptor table, and you can use a number which indexes one of those entries: 0 is STDIN, 1 is STDOUT, 2 is STDERR ... the remainder you open yourself.

        See Wikipedia's explanation.

        As Occam said: Entia non sunt multiplicanda praeter necessitatem.

        A file descriptor is just a number local to your program, in effect enumerating your open files. Well known file descriptors are 0 (usually pointing to STDIN), 1 (STDOUT) and 2 (STDERR)

        I don't see any advantage in this case, but I'm no expert on this. Maybe it was a miscomunication and he just meant to use $fd filehandles instead of FD filehandles!?

Re: Difference between File Handles and File Descriptors in function parameters.
by zentara (Archbishop) on Aug 11, 2011 at 16:09 UTC
    From my limited experience, filedescriptors ( the actual filehandle numbers) are useful because threads can access any open file under the same pid, via the fileno. Filehandles themselves are just a convenience on top of the file descriptors. See[threads] Open a file in one thread and allow others to write to it and FileHandles and threads. Everytime you open a filehandle, you get a filedesciptor underneath. On linux, you can look at /proc/$pid/fd and see all your filedescriptors sitting right there. You can use this fact to write to all open fd's you own, even from another process.

    There may be use also, in passing filedescriptors off to forked processes. You just can't pass a filehandle, but a descriptor yes.

    Other uses: to close ALL open filehandles, after having lost track of them. See How to close all open file descriptors after a fork? or the process of Duping filehandles Duping filehandles

    So anyways, don't blow off filedescriptors as useless.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Difference between File Handles and File Descriptors in function parameters.
by armstd (Friar) on Aug 11, 2011 at 18:23 UTC

    One downside I can think of here is, if you accept a passed-in filehandle, and use it directly, then your method will be altering the file position of the caller's handle. Your method will also be subject to the current position and any settings on the caller's filehandle, so "printRestOfFile()" might be a more accurate name than "printfile()" for your sub as shown.

    By accepting a descriptor, you would then make your own handle, with whatever settings that are appropriate for your method, only affecting your method, not the caller. This becomes much more of a concern when you're abstracting something, rather than just creating a worker sub for a structured script. Do no harm to your caller and all that.

    --Dave

      One downside I can think of here is, if you accept a passed-in filehandle, and use it directly, then your method will be altering the file position of the caller's handle.

      Thatís not a bug, itís a feature!

      It has to work this way, because otherwise you will get mangled I/O. It is critical that the seek points be the same or else you get overwrites and duplicate reads on seekable devices. You will also incur behavioral differences between running on nonseekable devices like sockets, pipes, and ttys then when running on seekable devices like disk files. Completely misery and chaos would ensue.

      Never assume that you alone have the sole copy of a particular kernel file descriptor. If you are relying on that, you are almost certainly broken in some way. Passed in descriptors are quite possibly duplicated in another process; with the std streams, this is virtually guaranteed.

      In fact, even if you open a file yourself, you cannot know you have the only copy because you cannot guarantee that you are not yourself a clone, since any function that you call after the open is welcome to fork itself an identical process. You cannot know you are who you think you are ó or least, not easily.

      Welcome to Unix, and have a nice day.

      --tom

Re: Difference between File Handles and File Descriptors in function parameters.
by cdarke (Prior) on Aug 12, 2011 at 08:52 UTC
    There is a source of further confusion. On UNIX the low-level deals with file descriptors, and Perl's structure for accessing them is through Perl file handles.

    However, on Windows file descriptors are called file handles! Wonderful! So far as I an tell, the only difference in that case, in C, is that on UNIX they are signed int and on Windows unsigned int (although wrapped in a Win type called HANDLE). So personally I take to referring to "Perl File Handles", rather than just plain "File Handles".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://919882]
Approved by toolic
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-09-23 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls