IO::Wrap − wrap raw filehandles in IO::Handle interface


   use IO::Wrap;
   ### Do stuff with any kind of filehandle (including a bare globref), or
   ### any kind of blessed object that responds to a print() message.
   sub do_stuff {
       my $fh = shift;
       ### At this point, we have no idea what the user gave us...
       ### a globref? a FileHandle? a scalar filehandle name?
       $fh = wraphandle($fh);
       ### At this point, we know we have an IO::Handle−like object!
       $fh−>print("Hey there!");


Let’s say you want to write some code which does I/O, but you don’t want to force the caller to provide you with a FileHandle or IO::Handle object. You want them to be able to say:


And even:


Sure, one way to do it is to force the caller to use tiehandle(). But that puts the burden on them. Another way to do it is to use IO::Wrap, which provides you with the following functions:
wraphandle SCALAR

This function will take a single argument, and "wrap" it based on what it seems to be...

A raw scalar filehandle name, like "STDOUT" or "Class::HANDLE". In this case, the filehandle name is wrapped in an IO::Wrap object, which is returned.

A raw filehandle glob, like "\*STDOUT". In this case, the filehandle glob is wrapped in an IO::Wrap object, which is returned.

A blessed FileHandle object. In this case, the FileHandle is wrapped in an IO::Wrap object if and only if your FileHandle class does not support the "read()" method.

Any other kind of blessed object, which is assumed to be already conformant to the IO::Handle interface. In this case, you just get back that object.

If you get back an IO::Wrap object, it will obey a basic subset of the IO:: interface. That is, the following methods (note: I said methods, not named operators) should work on the thing you get back:

    print ARGS...
    seek POS,WHENCE


Clearly, when wrapping a raw external filehandle (like \*STDOUT), I didn’t want to close the file descriptor when the "wrapper" object is destroyed... since the user might not appreciate that! Hence, there’s no DESTROY method in this class.

When wrapping a FileHandle object, however, I believe that Perl will invoke the FileHandle::DESTROY when the last reference goes away, so in that case, the filehandle is closed if the wrapped FileHandle really was the last reference to it.


This module does not allow you to wrap filehandle names which are given as strings that lack the package they were opened in. That is, if a user opens FOO in package Foo, they must pass it to you either as "\*FOO" or as "Foo::FOO". However, "STDIN" and friends will work just fine.


$Id:,v 1.2 2005/02/10 21:21:53 dfs Exp $


Primary Maintainer

David F. Skoll (

Original Author

Eryq ( President, ZeeGee Software Inc (


Hey! The above document had some coding errors, which are explained below:
Around line 212:

’=item’ outside of any ’=over’


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.