{
    "mode": "perldoc",
    "parameter": "FileHandle",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/FileHandle/json",
    "generated": "2026-06-13T19:59:36Z",
    "synopsis": "use FileHandle;\n$fh = FileHandle->new;\nif ($fh->open(\"< file\")) {\nprint <$fh>;\n$fh->close;\n}\n$fh = FileHandle->new(\"> FOO\");\nif (defined $fh) {\nprint $fh \"bar\\n\";\n$fh->close;\n}\n$fh = FileHandle->new(\"file\", \"r\");\nif (defined $fh) {\nprint <$fh>;\nundef $fh;       # automatically closes the file\n}\n$fh = FileHandle->new(\"file\", OWRONLY|OAPPEND);\nif (defined $fh) {\nprint $fh \"corge\\n\";\nundef $fh;       # automatically closes the file\n}\n$pos = $fh->getpos;\n$fh->setpos($pos);\n$fh->setvbuf($buffervar, IOLBF, 1024);\n($readfh, $writefh) = FileHandle::pipe;\nautoflush STDOUT 1;",
    "sections": {
        "NAME": {
            "content": "FileHandle - supply object methods for filehandles\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use FileHandle;\n\n$fh = FileHandle->new;\nif ($fh->open(\"< file\")) {\nprint <$fh>;\n$fh->close;\n}\n\n$fh = FileHandle->new(\"> FOO\");\nif (defined $fh) {\nprint $fh \"bar\\n\";\n$fh->close;\n}\n\n$fh = FileHandle->new(\"file\", \"r\");\nif (defined $fh) {\nprint <$fh>;\nundef $fh;       # automatically closes the file\n}\n\n$fh = FileHandle->new(\"file\", OWRONLY|OAPPEND);\nif (defined $fh) {\nprint $fh \"corge\\n\";\nundef $fh;       # automatically closes the file\n}\n\n$pos = $fh->getpos;\n$fh->setpos($pos);\n\n$fh->setvbuf($buffervar, IOLBF, 1024);\n\n($readfh, $writefh) = FileHandle::pipe;\n\nautoflush STDOUT 1;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "NOTE: This class is now a front-end to the IO::* classes.\n\n\"FileHandle::new\" creates a \"FileHandle\", which is a reference to a newly created symbol (see\nthe \"Symbol\" package). If it receives any parameters, they are passed to \"FileHandle::open\"; if\nthe open fails, the \"FileHandle\" object is destroyed. Otherwise, it is returned to the caller.\n\n\"FileHandle::newfromfd\" creates a \"FileHandle\" like \"new\" does. It requires two parameters,\nwhich are passed to \"FileHandle::fdopen\"; if the fdopen fails, the \"FileHandle\" object is\ndestroyed. Otherwise, it is returned to the caller.\n\n\"FileHandle::open\" accepts one parameter or two. With one parameter, it is just a front end for\nthe built-in \"open\" function. With two parameters, the first parameter is a filename that may\ninclude whitespace or other special characters, and the second parameter is the open mode,\noptionally followed by a file permission value.\n\nIf \"FileHandle::open\" receives a Perl mode string (\">\", \"+<\", etc.) or a POSIX fopen() mode\nstring (\"w\", \"r+\", etc.), it uses the basic Perl \"open\" operator.\n\nIf \"FileHandle::open\" is given a numeric mode, it passes that mode and the optional permissions\nvalue to the Perl \"sysopen\" operator. For convenience, \"FileHandle::import\" tries to import the\nOXXX constants from the Fcntl module. If dynamic loading is not available, this may fail, but\nthe rest of FileHandle will still work.\n\n\"FileHandle::fdopen\" is like \"open\" except that its first parameter is not a filename but rather\na file handle name, a FileHandle object, or a file descriptor number.\n\nIf the C functions fgetpos() and fsetpos() are available, then \"FileHandle::getpos\" returns an\nopaque value that represents the current position of the FileHandle, and \"FileHandle::setpos\"\nuses that value to return to a previously visited position.\n\nIf the C function setvbuf() is available, then \"FileHandle::setvbuf\" sets the buffering policy\nfor the FileHandle. The calling sequence for the Perl function is the same as its C counterpart,\nincluding the macros \"IOFBF\", \"IOLBF\", and \"IONBF\", except that the buffer parameter\nspecifies a scalar variable to use as a buffer. WARNING: A variable used as a buffer by\n\"FileHandle::setvbuf\" must not be modified in any way until the FileHandle is closed or until\n\"FileHandle::setvbuf\" is called again, or memory corruption may result!\n\nSee perlfunc for complete descriptions of each of the following supported \"FileHandle\" methods,\nwhich are just front ends for the corresponding built-in functions:\n\nclose\nfileno\ngetc\ngets\neof\nclearerr\nseek\ntell\n\nSee perlvar for complete descriptions of each of the following supported \"FileHandle\" methods:\n\nautoflush\noutputfieldseparator\noutputrecordseparator\ninputrecordseparator\ninputlinenumber\nformatpagenumber\nformatlinesperpage\nformatlinesleft\nformatname\nformattopname\nformatlinebreakcharacters\nformatformfeed\n\nFurthermore, for doing normal I/O you might need these:\n\n$fh->print\nSee \"print\" in perlfunc.\n\n$fh->printf\nSee \"printf\" in perlfunc.\n\n$fh->getline\nThis works like <$fh> described in \"I/O Operators\" in perlop except that it's more readable\nand can be safely called in a list context but still returns just one line.\n\n$fh->getlines\nThis works like <$fh> when called in a list context to read all the remaining lines in a\nfile, except that it's more readable. It will also croak() if accidentally called in a\nscalar context.\n\nThere are many other functions available since FileHandle is descended from IO::File,\nIO::Seekable, and IO::Handle. Please see those respective pages for documentation on more\nfunctions.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "The IO extension, perlfunc, \"I/O Operators\" in perlop.\n",
            "subsections": []
        }
    },
    "summary": "FileHandle - supply object methods for filehandles",
    "flags": [],
    "examples": [],
    "see_also": []
}