Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How do I create an IO::All object from a pre-existing filehandle

by DrWhy (Chaplain)
on Jun 11, 2009 at 00:34 UTC ( [id://770478]=perlquestion: print w/replies, xml ) Need Help??

DrWhy has asked for the wisdom of the Perl Monks concerning the following question:

Or perhaps the question is, is it possible at all? I would like to create a function that can take a file name or a previously opened generic perl filehandle and turn that into and IO::All object for processing. I haven't been able to create a usable IO::All object from a filehandle to this point. Here are some of the scenarios I've tried:

use IO::All; use IO::File; open G, '<test.txt'; $h = IO::File->new_from_fd(fileno(*G{IO}), 'r'); $i = io($h); print $i->getline

The docs of IO::All seem to imply that this should work, but when I run this (Win XP, ActiveState Perl 5.8.8, IO:All v0.33), I get:

Can't call method "opened" on an undefined value at C:/perl/site/lib/I +O/All/File.pm line 78.

I've also tried variations without the IO::File intermediary, i.e., $i = io(*G{IO}), $i = io(\*G), $i = io->handle(*G), all to no avail.

Has anyone been down the path before and have a workable solution to this?

Update: This is now in RT.

Thanks,

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Replies are listed 'Best First'.
Re: How do I create an IO::All object from a pre-existing filehandle
by almut (Canon) on Jun 11, 2009 at 16:05 UTC

    The problem apparently is that it is being checked if $self->_handle is defined, but then ->opened is being called on $self->io_handle, which is undefined in your case.

    if (defined $self->pathname) { $self->io_handle(IO::File->new); $self->io_handle->open($self->pathname, @args) or $self->throw($self->open_msg); } elsif (defined $self->_handle and not $self->io_handle->opened # <-- line 88 ) { # XXX Not tested $self->io_handle->fdopen($self->_handle, @args); }

    I think that piece of code should be

    ... elsif (defined $self->_handle) { if ($self->_handle->opened) { # already open ? --> set/use it $self->io_handle($self->_handle); } else { # not open --> open it # XXX Not tested $self->io_handle->fdopen($self->_handle, @args); } }

    At least that works for me with your test case.  Not sure about the else "XXX Not tested" branch, though... (probably still wrong, because I suppose the ->fdopen() would fail as well (just like ->opened), in case $self->io_handle is undefined).

Re: How do I create an IO::All object from a pre-existing filehandle
by ikegami (Patriarch) on Jun 11, 2009 at 18:50 UTC
    GIGO. An IO::File object is not a valid argument. "Only" the following are acceptable:
    • A file name
    • A file handle
    • A directory name
    • A directory handle
    • A typeglob reference
    • A piped shell command. eq '| ls -al'
    • A socket domain/port. eg 'perl.com:5678'
    • '-' means STDIN or STDOUT (depending on usage)
    • '=' means STDERR
    • '$' means an IO::String object
    • '?' means a temporary file
    • A URI including: http, https, ftp and mailto
    • An IO::All object

    Update: Then again, passing *G or \*G doesn't work either. And neither does io->handle($h). And it's not specific to Windows.

    Looks like a bug.

Re: How do I create an IO::All object from a pre-existing filehandle
by Anonymous Monk on Jun 11, 2009 at 00:39 UTC
    IO:All v0.33)

    Try upgrading

      I suppose I could, but I've reviewed the release notes for version 0.34 to 0.39 (latest) and none lists any changes that would be relevant, so I doubt an upgrade would be useful in this case.

      --DrWhy

      "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

        so I doubt an upgrade would be useful in this case.

        Do it anyway.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://770478]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2025-03-17 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (54 votes). Check out past polls.