{
    "mode": "perldoc",
    "parameter": "IO::HTML",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/IO%3A%3AHTML/json",
    "generated": "2026-06-10T16:16:07Z",
    "synopsis": "use IO::HTML;                 # exports htmlfile by default\nuse HTML::TreeBuilder;\nmy $tree = HTML::TreeBuilder->newfromfile(\nhtmlfile('foo.html')\n);\n# Alternative interface:\nopen(my $in, '<:raw', 'bar.html');\nmy $encoding = IO::HTML::sniffencoding($in, 'bar.html');",
    "sections": {
        "NAME": {
            "content": "IO::HTML - Open an HTML file with automatic charset detection\n",
            "subsections": []
        },
        "VERSION": {
            "content": "This document describes version 1.004 of IO::HTML, released September 26, 2020.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use IO::HTML;                 # exports htmlfile by default\nuse HTML::TreeBuilder;\n\nmy $tree = HTML::TreeBuilder->newfromfile(\nhtmlfile('foo.html')\n);\n\n# Alternative interface:\nopen(my $in, '<:raw', 'bar.html');\nmy $encoding = IO::HTML::sniffencoding($in, 'bar.html');\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "IO::HTML provides an easy way to open a file containing HTML while automatically determining its\nencoding. It uses the HTML5 encoding sniffing algorithm specified in section 8.2.2.2 of the\ndraft standard.\n\nThe algorithm as implemented here is:\n\n1.  If the file begins with a byte order mark indicating UTF-16LE, UTF-16BE, or UTF-8, then that\nis the encoding.\n\n2.  If the first $bytestocheck bytes of the file contain a \"<meta>\" tag that indicates the\ncharset, and Encode recognizes the specified charset name, then that is the encoding. (This\nportion of the algorithm is implemented by \"findcharsetin\".)\n\nThe \"<meta>\" tag can be in one of two formats:\n\n<meta charset=\"...\">\n<meta http-equiv=\"Content-Type\" content=\"...charset=...\">\n\nThe search is case-insensitive, and the order of attributes within the tag is irrelevant.\nAny additional attributes of the tag are ignored. The first matching tag with a recognized\nencoding ends the search.\n\n3.  If the first $bytestocheck bytes of the file are valid UTF-8 (with at least 1 non-ASCII\ncharacter), then the encoding is UTF-8.\n\n4.  If all else fails, use the default character encoding. The HTML5 standard suggests the\ndefault encoding should be locale dependent, but currently it is always \"cp1252\" unless you\nset $IO::HTML::defaultencoding to a different value. Note: \"sniffencoding\" does not apply\nthis step; only \"htmlfile\" does that.\n",
            "subsections": []
        },
        "SUBROUTINES": {
            "content": "htmlfile\n$filehandle = htmlfile($filename, \\%options);\n\nThis function (exported by default) is the primary entry point. It opens the file specified by\n$filename for reading, uses \"sniffencoding\" to find a suitable encoding layer, and applies it.\nIt also applies the \":crlf\" layer. If the file begins with a BOM, the filehandle is positioned\njust after the BOM.\n\nThe optional second argument is a hashref containing options. The possible keys are described\nunder \"findcharsetin\".\n\nIf \"sniffencoding\" is unable to determine the encoding, it defaults to\n$IO::HTML::defaultencoding, which is set to \"cp1252\" (a.k.a. Windows-1252) by default.\nAccording to the standard, the default should be locale dependent, but that is not currently\nimplemented.\n\nIt dies if the file cannot be opened, or if \"sniffencoding\" cannot determine the encoding and\n$IO::HTML::defaultencoding has been set to \"undef\".\n\nhtmlfileandencoding\n($filehandle, $encoding, $bom)\n= htmlfileandencoding($filename, \\%options);\n\nThis function (exported only by request) is just like \"htmlfile\", but returns more information.\nIn addition to the filehandle, it returns the name of the encoding used, and a flag indicating\nwhether a byte order mark was found (if $bom is true, the file began with a BOM). This may be\nuseful if you want to write the file out again (especially in conjunction with the\n\"htmloutfile\" function).\n\nThe optional second argument is a hashref containing options. The possible keys are described\nunder \"findcharsetin\".\n\nIt dies if the file cannot be opened, or if \"sniffencoding\" cannot determine the encoding and\n$IO::HTML::defaultencoding has been set to \"undef\".\n\nThe result of calling \"htmlfileandencoding\" in scalar context is undefined (in the C sense of\nthere is no guarantee what you'll get).\n\nhtmloutfile\n$filehandle = htmloutfile($filename, $encoding, $bom);\n\nThis function (exported only by request) opens $filename for output using $encoding, and writes\na BOM to it if $bom is true. If $encoding is \"undef\", it defaults to\n$IO::HTML::defaultencoding. $encoding may be either an encoding name or an Encode::Encoding\nobject.\n\nIt dies if the file cannot be opened, or if both $encoding and $IO::HTML::defaultencoding are\n\"undef\".\n\nsniffencoding\n($encoding, $bom) = sniffencoding($filehandle, $filename, \\%options);\n\nThis function (exported only by request) runs the HTML5 encoding sniffing algorithm on\n$filehandle (which must be seekable, and should have been opened in \":raw\" mode). $filename is\nused only for error messages (if there's a problem using the filehandle), and defaults to \"file\"\nif omitted. The optional third argument is a hashref containing options. The possible keys are\ndescribed under \"findcharsetin\".\n\nIt returns Perl's canonical name for the encoding, which is not necessarily the same as the MIME\nor IANA charset name. It returns \"undef\" if the encoding cannot be determined. $bom is true if\nthe file began with a byte order mark. In scalar context, it returns only $encoding.\n\nThe filehandle's position is restored to its original position (normally the beginning of the\nfile) unless $bom is true. In that case, the position is immediately after the BOM.\n\nTip: If you want to run \"sniffencoding\" on a file you've already loaded into a string, open an\nin-memory file on the string, and pass that handle:\n\n($encoding, $bom) = do {\nopen(my $fh, '<', \\$string);  sniffencoding($fh)\n};\n\n(This only makes sense if $string contains bytes, not characters.)\n\nfindcharsetin\n$encoding = findcharsetin($stringcontainingHTML, \\%options);\n\nThis function (exported only by request) looks for charset information in a \"<meta>\" tag in a\npossibly-incomplete HTML document using the \"two step\" algorithm specified by HTML5. It does not\nlook for a BOM. The \"<meta>\" tag must begin within the first $IO::HTML::bytestocheck bytes of\nthe string.\n\nIt returns Perl's canonical name for the encoding, which is not necessarily the same as the MIME\nor IANA charset name. It returns \"undef\" if no charset is specified or if the specified charset\nis not recognized by the Encode module.\n\nThe optional second argument is a hashref containing options. The following keys are recognized:\n\n\"encoding\"\nIf true, return the Encode::Encoding object instead of its name. Defaults to false.\n\n\"needpragma\"\nIf true (the default), follow the HTML5 spec and examine the \"content\" attribute only of\n\"<meta http-equiv=\"Content-Type\"\". If set to 0, relax the HTML5 spec, and look for\n\"charset=\" in the \"content\" attribute of *every* meta tag.\n",
            "subsections": []
        },
        "EXPORTS": {
            "content": "By default, only \"htmlfile\" is exported. Other functions may be exported on request.\n\nFor people who prefer not to export functions, all functions beginning with \"html\" have an\nalias without that prefix (e.g. you can call \"IO::HTML::file(...)\" instead of\n\"IO::HTML::htmlfile(...)\". These aliases are not exportable.\n\nThe following export tags are available:\n\n\":all\"\nAll exportable functions.\n\n\":rw\"\n\"htmlfile\", \"htmlfileandencoding\", \"htmloutfile\".\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "The HTML5 specification, section 8.2.2.2 Determining the character encoding:\n<http://www.w3.org/TR/html5/syntax.html#determining-the-character-encoding>\n",
            "subsections": []
        },
        "DIAGNOSTICS": {
            "content": "\"Could not read %s: %s\"\nThe specified file could not be read from for the reason specified by $!.\n\n\"Could not seek %s: %s\"\nThe specified file could not be rewound for the reason specified by $!.\n\n\"Failed to open %s: %s\"\nThe specified file could not be opened for reading for the reason specified by $!.\n\n\"No default encoding specified\"\nThe \"sniffencoding\" algorithm didn't find an encoding to use, and you set\n$IO::HTML::defaultencoding to \"undef\".\n",
            "subsections": []
        },
        "CONFIGURATION AND ENVIRONMENT": {
            "content": "There are two global variables that affect IO::HTML. If you need to change them, you should do\nso using \"local\" if possible:\n\nmy $file = do {\n# This file may define the charset later in the header\nlocal $IO::HTML::bytestocheck = 4096;\nhtmlfile(...);\n};\n\n$bytestocheck\nThis is the number of bytes that \"sniffencoding\" will read from the stream. It is also the\nnumber of bytes that \"findcharsetin\" will search for a \"<meta>\" tag containing charset\ninformation. It must be a positive integer.\n\nThe HTML 5 specification recommends using the default value of 1024, but some pages do not\nfollow the specification.\n\n$defaultencoding\nThis is the encoding that \"htmlfile\" and \"htmlfileandencoding\" will use if no encoding\ncan be detected by \"sniffencoding\". The default value is \"cp1252\" (a.k.a. Windows-1252).\n\nSetting it to \"undef\" will cause the file subroutines to croak if \"sniffencoding\" fails to\ndetermine the encoding. (\"sniffencoding\" itself does not use $defaultencoding).\n",
            "subsections": []
        },
        "DEPENDENCIES": {
            "content": "IO::HTML has no non-core dependencies for Perl 5.8.7+. With earlier versions of Perl 5.8, you\nneed to upgrade Encode to at least version 2.10, and you may need to upgrade Exporter to at\nleast version 5.57.\n",
            "subsections": []
        },
        "INCOMPATIBILITIES": {
            "content": "None reported.\n",
            "subsections": []
        },
        "BUGS AND LIMITATIONS": {
            "content": "No bugs have been reported.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Christopher J. Madsen \"<perl AT cjmweb.net>\"\n\nPlease report any bugs or feature requests to \"<bug-IO-HTML AT rt.cpan.org>\" or through the web\ninterface at <http://rt.cpan.org/Public/Bug/Report.html?Queue=IO-HTML>.\n\nYou can follow or contribute to IO-HTML's development at <https://github.com/madsen/io-html>.\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2020 by Christopher J. Madsen.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        },
        "DISCLAIMER OF WARRANTY": {
            "content": "BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE\nEXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\nHOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER\nEXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY\nAND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE\nSOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY\nSERVICING, REPAIR, OR CORRECTION.\n\nIN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER,\nOR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE\nLICENSE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR\nCONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT\nLIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD\nPARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR\nOTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\n",
            "subsections": []
        }
    },
    "summary": "IO::HTML - Open an HTML file with automatic charset detection",
    "flags": [],
    "examples": [],
    "see_also": []
}