in reply to Re^7: Should Modules Do I/O?
in thread Should Modules Do I/O?
I think I'll address your post a little backwards. "Stupidity"? Communication is a two-way street, and thus it's rarely one-sided on a failure. In this case, I think I have to shoulder more of the blame of your misunderstanding than you do ;-)
I had it my head the example of Archive::Tar. I'm not entirely sure why (was there a recent thread on this module or something that could be sticking in my head?). So when I say "$obj->get_file", I'm thinking that we're telling the "$obj"ect to get a file from the archive (or, more generally, from whatever source it is encapsulating, e.g., a cache, an FTP server, a web server, a zip file, a jar file, whatever). In this scenario, in all likeliness, well over 80% of the time, you want to get the file from the encapsulated location, and put it somewhere locally. This is where having a simple function that does I/O makes sense. For the other < 20% of the time, I agree that the file handle interface makes the most sense (some encapsulated data types may make more sense to use the callback, e.g., sockets such as FTP, where you still want the object to handle the communication with the remote server, and the overhead of creating a tie'd handle may be too much to bother with, since the end of the file doesn't actually correspond with the end of data through the socket).
Perhaps a more elaborate set of object methods would be useful:
All of these have no return value (as I was thinking for the get_file/get_filehandle above). Yes, with perl, often you can overload these to be the same function which can dynamically figure out the difference between a file name and a filehandle. Sometimes that's not feasable, other times ... well, I'm separating it out here purely to show the dual interface of which I'm supportive.sub extract_file # takes a destination directory or filename sub extract_file_via_filehandle # takes a filehandle to write to sub insert_file # takes a source directory and filename sub insert_file_via_filehandle # takes a filename (for inside the arch +ive) and a filehandle (for the source)
You do bring up an interesting question: a way to get a filehandle to which one can write. Unfortunately, that may mean some sort of tied interface - this to allow further action with it (imagine FTP where the connection needs to be maintained, thus closing the filehandle should be somehow prevented, or a dynamic archive where one day you could be writing to a filesystem, another to an FTP server, and another to a RDBMS - some of these would need something tied, such as saving to a blob in a database via chunks). I may have to pursue that myself, actually. It may be a cleaner interface. Thanks ;-)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^9: Should Modules Do I/O?
by BrowserUk (Patriarch) on Mar 19, 2005 at 07:25 UTC | |
by Tanktalus (Canon) on Mar 19, 2005 at 14:44 UTC | |
by BrowserUk (Patriarch) on Mar 19, 2005 at 14:53 UTC | |
by Tanktalus (Canon) on Mar 19, 2005 at 15:06 UTC | |
by BrowserUk (Patriarch) on Mar 19, 2005 at 17:09 UTC | |
| |
by revdiablo (Prior) on Mar 19, 2005 at 18:38 UTC |