in reply to Should Modules Do I/O?
I think you asked a very good question and are thinking along the right lines.
As counterpoint to other responses, I hate modules that insist on writing stuff to disk, and force me to re-open the output file* in order to get the data back into my program.
Maybe
So, I'd infinitely prefer an interface that allowed me to supply an open filehandle for the archive file (new or existing), and methods for adding and retrieving files from the archive:
Reading
use Your::Module; open my $arc, 'ftp ftp://some.dot.com/pub/3GB.archive |' or die; local $/ = \65536; ## Could your module handle this? my $arcObj = Your::Module->new( $arc ); while( my( $name, $dataRef ) = $arcObj->next ) { if( $name =~ m[^file(\d+.type)$] and $$dataRef =~ m[this|that] ){ open $out, '>', localtime . $1 or die $!; print $out $$dataRef; last } }
Writing
use Win32API::File qw[ :all ]; use Your::Module; my $hObject = CreateFile( '//?/UNC/Server/Share/Dir/File.Ext', FILE_READ_EA, FILE_SHARE_READ, pack( "L P i", 12, $pSecDesc, $bInheritHandle ), TRUNCATE_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN|FILE_FLAG_WRITE_THROUGH, SECURITY_IDENTIFICATION|SECURITY_IMPERSONATION, 0 ) or die $^E; OsFHandleOpen( FILE, $hObject, $sMode ) or die $^E; my $arcObj = Your::Module->new( \*FILE ); opendir DIR, '//SERVER/DIR/'; while( my $file = readdir DIR ) { open my $fh, '<', $file or die $!; $arcObj->addFile( "/DIR/$file", do{ local $/; <$fh> } ); } close DIR;
Providing that kind of flexibility for the users, combined with the reduction in code in your module, would make your module more powerful and useful.
Especially as you're providing 'arcit.pl' and 'unarcit.pl' scripts for the simple case, thereby avoiding the "boilerplate code" charge.
(*usually after patching the module to provide a way of finding out the filename)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Should Modules Do I/O?
by Mugatu (Monk) on Mar 18, 2005 at 18:54 UTC | |
by BrowserUk (Patriarch) on Mar 18, 2005 at 19:06 UTC | |
by Mugatu (Monk) on Mar 18, 2005 at 20:57 UTC | |
by BrowserUk (Patriarch) on Mar 18, 2005 at 21:45 UTC | |
by Mugatu (Monk) on Mar 18, 2005 at 22:17 UTC | |
by Tanktalus (Canon) on Mar 18, 2005 at 21:54 UTC | |
by BrowserUk (Patriarch) on Mar 18, 2005 at 22:12 UTC | |
by Tanktalus (Canon) on Mar 18, 2005 at 22:51 UTC | |
|