[Melbourne-pm] IO::File with Capture for Quota

Paul Fenwick pjf at perltraining.com.au
Wed Oct 15 20:53:26 PDT 2008


G'day Scott,

Scott Penrose wrote:

> 1) Is there anyway of getting from a file handle:
>     - That it is a file on disk, rather than Socket or Pipe

I imagine yes.  The stat() call returns the device number, inode number, and
file mode.  For a network socket, I imagine the device and inode number will
be set to funny values.  For a named pipe on the disk, I imagine the file
mode will contain a hint.

>     - The filename of that file

As far as I know, no, at least not on Unix-flavoured systems.  After the
file has been opened, the file can be renamed, or even deleted.  In fact,
after you've opened it, you can't even rely upon a file of the same name
referring to the same file; for example, when the file gets rotated and a
new file dropped in its place.

Luckily, the stat() call will give you both size and inode number.  If the
inode number has changed, you know that the file have been
roated/moved/deleted/etc.  Of course, if you're passing filehandles to stat,
you'll always be examining the correct file, even if the name has changed.

> 2) What else would you change - in functions, calls and purpose to make
> this a useful CPAN module

I don't like the idea of a global callback, I'm much fonder of having
per-object callbacks:

	IO::File::Quota->new('>> /tmp/example', \&callback);

If something doesn't have a callback (or even if it does), I'd change the
returns of close to return the change in size in bytes, or "0 but true" or
some equivalent value if the close was successful, but the file didn't
change in length.  That allows for code like:

	my $fh = IO::File::Quote->new('>> /tmp/example');

	while (<STDIN>) {
		$fh->print($_);
	}

	my $bytes_read = $fh->close or die "Uh oh!  $!";

I'd also add a '->count' method, so I can tell how many bytes have been
added (or removed) to the file so far.  This can just be syntactic sugar for
flushing the file and counting its size.

I'd also change the name.  If we're not trying to enforce a quota, we just
want a change in size, then I'd consider IO::File::SizeDelta or similar as a
name.

> 3) Is there already something like this you know of?

Nope, but I haven't looked.  I usually ask modules at perl.org such things.

Cheerio,

	Paul

-- 
Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681


More information about the Melbourne-pm mailing list