Hey,<br><br>Seems to be working for me:<br><br>-- TestFh.pm --<br><br> package TestFh;<br><br> use strict;<br> use warnings;<br><br> use IO::File;<br><br> sub test_fh {<br> my ( $fh ) = @_;<br> printf "fileno: %d\n", $fh->fileno();<br>
}<br><br> 1;<br><br>-- <a href="http://test.pl">test.pl</a> --<br><br> #!/usr/bin/perl<br><br> use strict;<br> use warnings;<br><br> use TestFh;<br><br> open my $fh, ">test.txt"<br> or die $!;<br>
<br> TestFh::test_fh($fh);<br><br>-- Output --<br><br> $ perl <a href="http://test.pl">test.pl</a><br> fileno: 3<br><br>So, unless I'm not understanding the problem, maybe Moose is getting in the way? Try writing a small test script to get what you want and then see if IO::Handle magic kicks in. Otherwise, i'm out of ideas :(<br>
<br>Alfie<br><br><div class="gmail_quote">On Tue, Jan 12, 2010 at 6:09 PM, Toby Corkindale <span dir="ltr"><<a href="mailto:toby.corkindale@strategicdata.com.au">toby.corkindale@strategicdata.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">Alfie John wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hey Toby,<br>
<br>
When you use IO::Handle etc, all regular handles are automagically blessed into IO::Handle objects:<br>
<br>
-- 8< --<br>
<br>
$ perl -we 'use IO::File; open my $fh, ">test.txt"; $fh->print( "hello world" )'<br>
$ cat test.txt<br>
Hello world<br>
<br>
-- >8 --<br>
<br>
Pretty elegant huh :)<br>
</blockquote>
<br></div>
I had seen someone else mention that, but when I tried it, it failed - I think because you need to "use IO::File" in the calling class before opening the file -- and I don't have control over the calling code.<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
As for FileHandle, haven't used it. Would subclassing FileHandle do what you want? All that Moose stuff looks a bit ugly.<br>
</blockquote>
<br></div>
Hmm, same problem as above -- how to deal with not having control over the point where it's opened, just receiving the parameter into your code.<br>
<br>
thanks!<br>
Toby<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">
On Tue, Jan 12, 2010 at 5:29 PM, Toby Corkindale <<a href="mailto:toby.corkindale@strategicdata.com.au" target="_blank">toby.corkindale@strategicdata.com.au</a> <mailto:<a href="mailto:toby.corkindale@strategicdata.com.au" target="_blank">toby.corkindale@strategicdata.com.au</a>>> wrote:<br>
<br>
Old-school Perl file handles looked like this:<br>
<br>
open(INPUT, "<$filename");<br>
<br>
Then later they could be:<br>
<br>
open(my $input, "<$filename");<br>
<br>
New-school filehandles are:<br>
<br>
my $input = IO::File->new($filename, 'r');<br>
<br>
And interregnum filehandles were:<br>
<br>
my $fh = FileHandle->new($filename, 'r');<br>
<br>
I want to have a method which accepts all, but operates upon them<br>
using new-school object methods, ie. $file->autoflush(1) or<br>
$file->input_line_number;<br>
<br>
<br>
Currently I'm doing it via the following simplified code example,<br>
but I wondered if there was a more elegant solution?<br>
<br>
<br>
package Thingy;<br>
use Moose;<br>
use IO::Handle;<br>
<br>
has 'input' => (<br>
is => 'rw',<br>
isa => 'FileHandle', # Native Moose type, not same as FileHandle<br>
);<br>
<br>
around 'BUILDARGS' => sub {<br>
my ($orig, $class, $args) = @_;<br>
unless (blessed $args{input} and $args{input}->isa('IO::Handle')) {<br>
$args->{input} = IO::Handle->new->fdopen(fileno($args->{input}));<br>
}<br>
return $args;<br>
};<br>
_______________________________________________<br>
Melbourne-pm mailing list<br></div></div>
<a href="mailto:Melbourne-pm@pm.org" target="_blank">Melbourne-pm@pm.org</a> <mailto:<a href="mailto:Melbourne-pm@pm.org" target="_blank">Melbourne-pm@pm.org</a>><div class="im"><br>
<a href="http://mail.pm.org/mailman/listinfo/melbourne-pm" target="_blank">http://mail.pm.org/mailman/listinfo/melbourne-pm</a><br>
<br>
<br>
</div></blockquote><font color="#888888">
<br>
<br>
-- <br>
Strategic Data Pty Ltd<br>
Ph: 03 9340 9000<br>
</font></blockquote></div><br>