[Melbourne-pm] Promoting old-school file handles to io::handles
Toby Corkindale
toby.corkindale at strategicdata.com.au
Mon Jan 11 22:29:20 PST 2010
Old-school Perl file handles looked like this:
open(INPUT, "<$filename");
Then later they could be:
open(my $input, "<$filename");
New-school filehandles are:
my $input = IO::File->new($filename, 'r');
And interregnum filehandles were:
my $fh = FileHandle->new($filename, 'r');
I want to have a method which accepts all, but operates upon them using
new-school object methods, ie. $file->autoflush(1) or
$file->input_line_number;
Currently I'm doing it via the following simplified code example, but I
wondered if there was a more elegant solution?
package Thingy;
use Moose;
use IO::Handle;
has 'input' => (
is => 'rw',
isa => 'FileHandle', # Native Moose type, not same as FileHandle
);
around 'BUILDARGS' => sub {
my ($orig, $class, $args) = @_;
unless (blessed $args{input} and $args{input}->isa('IO::Handle')) {
$args->{input} = IO::Handle->new->fdopen(fileno($args->{input}));
}
return $args;
};
More information about the Melbourne-pm
mailing list