in reply to Should Modules Do I/O?

return a hashref with output names as keys, and file contents as data values (caller can write them out himself)

The only thing this would do is force most of your users to use the same boilerplate every time they used your module:

for (my($file,$data) = each %returnedhash) { open my $f, ">", $file or die "$!"; print $f $data; }

And we all know that boilerplate is generally bad. Modules are written to avoid boilerplate, not create more of it.

If you're afraid that your module might occasionally generate invalid data, and you don't want to clobber their files with it, I can sort of understand your reluctance. But believe me, if you provide them with data and a filename, they will clobber their own files happily, and still blame you for any problems that may occur. The only difference is that they will be cursing you for an inconvenient interface as well. :-)

If, on the other hand, you're confident in your module, then I don't see a problem. As long as the module is clearly documented to clobber files, you should have no reason to be concerned. To that end, your test suite should be heavily loaded towards pounding on the file IO code. This can at least provide a minimum of reassurance.

Update: of course, I agree with all the other fine Monks who recommend having both methods available.

Replies are listed 'Best First'.
Re^2: Should Modules Do I/O?
by pboin (Deacon) on Mar 18, 2005 at 17:35 UTC

    You know, that's exactly right (re: the boiler plate).

    I'd already thought, that that's what I'd do if I were the caller. But, I didn't extend that thought to that's what everyone would do, so why bother?

    Good point, and thanks. I'm working on the test suite now, just as an exercise to see how many ways I might want to call this thing.