{
    "mode": "perldoc",
    "parameter": "IO::Wrap",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/IO%3A%3AWrap/json",
    "generated": "2026-06-12T06:19:56Z",
    "synopsis": "use IO::Wrap;\n### Do stuff with any kind of filehandle (including a bare globref), or\n### any kind of blessed object that responds to a print() message.\n###\nsub dostuff {\nmy $fh = shift;\n### At this point, we have no idea what the user gave us...\n### a globref? a FileHandle? a scalar filehandle name?\n$fh = wraphandle($fh);\n### At this point, we know we have an IO::Handle-like object!\n$fh->print(\"Hey there!\");\n...\n}",
    "sections": {
        "NAME": {
            "content": "IO::Wrap - wrap raw filehandles in IO::Handle interface\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use IO::Wrap;\n\n### Do stuff with any kind of filehandle (including a bare globref), or\n### any kind of blessed object that responds to a print() message.\n###\nsub dostuff {\nmy $fh = shift;\n\n### At this point, we have no idea what the user gave us...\n### a globref? a FileHandle? a scalar filehandle name?\n\n$fh = wraphandle($fh);\n\n### At this point, we know we have an IO::Handle-like object!\n\n$fh->print(\"Hey there!\");\n...\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Let's say you want to write some code which does I/O, but you don't want to force the caller to\nprovide you with a FileHandle or IO::Handle object. You want them to be able to say:\n\ndostuff(\\*STDOUT);\ndostuff('STDERR');\ndostuff($someFileHandleobject);\ndostuff($someIOHandleobject);\n\nAnd even:\n\ndostuff($anyobjectwithaprintmethod);\n\nSure, one way to do it is to force the caller to use tiehandle(). But that puts the burden on\nthem. Another way to do it is to use IO::Wrap, which provides you with the following functions:\n\nwraphandle SCALAR\nThis function will take a single argument, and \"wrap\" it based on what it seems to be...\n\n*   A raw scalar filehandle name, like \"STDOUT\" or \"Class::HANDLE\". In this case, the\nfilehandle name is wrapped in an IO::Wrap object, which is returned.\n\n*   A raw filehandle glob, like \"\\*STDOUT\". In this case, the filehandle glob is wrapped in\nan IO::Wrap object, which is returned.\n\n*   A blessed FileHandle object. In this case, the FileHandle is wrapped in an IO::Wrap\nobject if and only if your FileHandle class does not support the \"read()\" method.\n\n*   Any other kind of blessed object, which is assumed to be already conformant to the\nIO::Handle interface. In this case, you just get back that object.\n\nIf you get back an IO::Wrap object, it will obey a basic subset of the IO:: interface. That is,\nthe following methods (note: I said *methods*, not named operators) should work on the thing you\nget back:\n\nclose\ngetline\ngetlines\nprint ARGS...\nread BUFFER,NBYTES\nseek POS,WHENCE\ntell\n",
            "subsections": []
        },
        "NOTES": {
            "content": "Clearly, when wrapping a raw external filehandle (like \\*STDOUT), I didn't want to close the\nfile descriptor when the \"wrapper\" object is destroyed... since the user might not appreciate\nthat! Hence, there's no DESTROY method in this class.\n\nWhen wrapping a FileHandle object, however, I believe that Perl will invoke the\nFileHandle::DESTROY when the last reference goes away, so in that case, the filehandle is closed\nif the wrapped FileHandle really was the last reference to it.\n",
            "subsections": []
        },
        "WARNINGS": {
            "content": "This module does not allow you to wrap filehandle names which are given as strings that lack the\npackage they were opened in. That is, if a user opens FOO in package Foo, they must pass it to\nyou either as \"\\*FOO\" or as \"Foo::FOO\". However, \"STDIN\" and friends will work just fine.\n",
            "subsections": []
        },
        "VERSION": {
            "content": "$Id: Wrap.pm,v 1.2 2005/02/10 21:21:53 dfs Exp $\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Primary Maintainer\nDianne Skoll (dfs@roaringpenguin.com).\n\nOriginal Author\nEryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).\n",
            "subsections": []
        }
    },
    "summary": "IO::Wrap - wrap raw filehandles in IO::Handle interface",
    "flags": [],
    "examples": [],
    "see_also": []
}