{
    "mode": "perldoc",
    "parameter": "IO::ScalarArray",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/IO%3A%3AScalarArray/json",
    "generated": "2026-06-10T16:22:15Z",
    "synopsis": "Perform I/O on strings, using the basic OO interface...\nuse IO::ScalarArray;\n@data = (\"My mes\", \"sage:\\n\");\n### Open a handle on an array, and append to it:\n$AH = new IO::ScalarArray \\@data;\n$AH->print(\"Hello\");\n$AH->print(\", world!\\nBye now!\\n\");\nprint \"The array is now: \", @data, \"\\n\";\n### Open a handle on an array, read it line-by-line, then close it:\n$AH = new IO::ScalarArray \\@data;\nwhile (defined($ = $AH->getline)) {\nprint \"Got line: $\";\n}\n$AH->close;\n### Open a handle on an array, and slurp in all the lines:\n$AH = new IO::ScalarArray \\@data;\nprint \"All lines:\\n\", $AH->getlines;\n### Get the current position (either of two ways):\n$pos = $AH->getpos;\n$offset = $AH->tell;\n### Set the current position (either of two ways):\n$AH->setpos($pos);\n$AH->seek($offset, 0);\n### Open an anonymous temporary array:\n$AH = new IO::ScalarArray;\n$AH->print(\"Hi there!\");\nprint \"I printed: \", @{$AH->aref}, \"\\n\";      ### get at value\nDon't like OO for your I/O? No problem. Thanks to the magic of an invisible tie(), the following\nnow works out of the box, just as it does with IO::Handle:\nuse IO::ScalarArray;\n@data = (\"My mes\", \"sage:\\n\");\n### Open a handle on an array, and append to it:\n$AH = new IO::ScalarArray \\@data;\nprint $AH \"Hello\";\nprint $AH \", world!\\nBye now!\\n\";\nprint \"The array is now: \", @data, \"\\n\";\n### Open a handle on a string, read it line-by-line, then close it:\n$AH = new IO::ScalarArray \\@data;\nwhile (<$AH>) {\nprint \"Got line: $\";\n}\nclose $AH;\n### Open a handle on a string, and slurp in all the lines:\n$AH = new IO::ScalarArray \\@data;\nprint \"All lines:\\n\", <$AH>;\n### Get the current position (WARNING: requires 5.6):\n$offset = tell $AH;\n### Set the current position (WARNING: requires 5.6):\nseek $AH, $offset, 0;\n### Open an anonymous temporary scalar:\n$AH = new IO::ScalarArray;\nprint $AH \"Hi there!\";\nprint \"I printed: \", @{$AH->aref}, \"\\n\";      ### get at value\nAnd for you folks with 1.x code out there: the old tie() style still works, though this is\n*unnecessary and deprecated*:\nuse IO::ScalarArray;\n### Writing to a scalar...\nmy @a;\ntie *OUT, 'IO::ScalarArray', \\@a;\nprint OUT \"line 1\\nline 2\\n\", \"line 3\\n\";\nprint \"Array is now: \", @a, \"\\n\"\n### Reading and writing an anonymous scalar...\ntie *OUT, 'IO::ScalarArray';\nprint OUT \"line 1\\nline 2\\n\", \"line 3\\n\";\ntied(OUT)->seek(0,0);\nwhile (<OUT>) {\nprint \"Got line: \", $;\n}",
    "sections": {
        "NAME": {
            "content": "IO::ScalarArray - IO:: interface for reading/writing an array of scalars\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "Perform I/O on strings, using the basic OO interface...\n\nuse IO::ScalarArray;\n@data = (\"My mes\", \"sage:\\n\");\n\n### Open a handle on an array, and append to it:\n$AH = new IO::ScalarArray \\@data;\n$AH->print(\"Hello\");\n$AH->print(\", world!\\nBye now!\\n\");\nprint \"The array is now: \", @data, \"\\n\";\n\n### Open a handle on an array, read it line-by-line, then close it:\n$AH = new IO::ScalarArray \\@data;\nwhile (defined($ = $AH->getline)) {\nprint \"Got line: $\";\n}\n$AH->close;\n\n### Open a handle on an array, and slurp in all the lines:\n$AH = new IO::ScalarArray \\@data;\nprint \"All lines:\\n\", $AH->getlines;\n\n### Get the current position (either of two ways):\n$pos = $AH->getpos;\n$offset = $AH->tell;\n\n### Set the current position (either of two ways):\n$AH->setpos($pos);\n$AH->seek($offset, 0);\n\n### Open an anonymous temporary array:\n$AH = new IO::ScalarArray;\n$AH->print(\"Hi there!\");\nprint \"I printed: \", @{$AH->aref}, \"\\n\";      ### get at value\n\nDon't like OO for your I/O? No problem. Thanks to the magic of an invisible tie(), the following\nnow works out of the box, just as it does with IO::Handle:\n\nuse IO::ScalarArray;\n@data = (\"My mes\", \"sage:\\n\");\n\n### Open a handle on an array, and append to it:\n$AH = new IO::ScalarArray \\@data;\nprint $AH \"Hello\";\nprint $AH \", world!\\nBye now!\\n\";\nprint \"The array is now: \", @data, \"\\n\";\n\n### Open a handle on a string, read it line-by-line, then close it:\n$AH = new IO::ScalarArray \\@data;\nwhile (<$AH>) {\nprint \"Got line: $\";\n}\nclose $AH;\n\n### Open a handle on a string, and slurp in all the lines:\n$AH = new IO::ScalarArray \\@data;\nprint \"All lines:\\n\", <$AH>;\n\n### Get the current position (WARNING: requires 5.6):\n$offset = tell $AH;\n\n### Set the current position (WARNING: requires 5.6):\nseek $AH, $offset, 0;\n\n### Open an anonymous temporary scalar:\n$AH = new IO::ScalarArray;\nprint $AH \"Hi there!\";\nprint \"I printed: \", @{$AH->aref}, \"\\n\";      ### get at value\n\nAnd for you folks with 1.x code out there: the old tie() style still works, though this is\n*unnecessary and deprecated*:\n\nuse IO::ScalarArray;\n\n### Writing to a scalar...\nmy @a;\ntie *OUT, 'IO::ScalarArray', \\@a;\nprint OUT \"line 1\\nline 2\\n\", \"line 3\\n\";\nprint \"Array is now: \", @a, \"\\n\"\n\n### Reading and writing an anonymous scalar...\ntie *OUT, 'IO::ScalarArray';\nprint OUT \"line 1\\nline 2\\n\", \"line 3\\n\";\ntied(OUT)->seek(0,0);\nwhile (<OUT>) {\nprint \"Got line: \", $;\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This class is part of the IO::Stringy distribution; see IO::Stringy for change log and general\ninformation.\n\nThe IO::ScalarArray class implements objects which behave just like IO::Handle (or FileHandle)\nobjects, except that you may use them to write to (or read from) arrays of scalars. Logically,\nan array of scalars defines an in-core \"file\" whose contents are the concatenation of the\nscalars in the array. The handles created by this class are automatically tiehandle'd (though\nplease see \"WARNINGS\" for information relevant to your Perl version).\n\nFor writing large amounts of data with individual print() statements, this class is likely to be\nmore efficient than IO::Scalar.\n\nBasically, this:\n\nmy @a;\n$AH = new IO::ScalarArray \\@a;\n$AH->print(\"Hel\", \"lo, \");         ### OO style\n$AH->print(\"world!\\n\");            ### ditto\n\nOr this:\n\nmy @a;\n$AH = new IO::ScalarArray \\@a;\nprint $AH \"Hel\", \"lo, \";           ### non-OO style\nprint $AH \"world!\\n\";              ### ditto\n\nCauses @a to be set to the following array of 3 strings:\n\n( \"Hel\" ,\n\"lo, \" ,\n\"world!\\n\" )\n\nSee IO::Scalar and compare with this class.\n",
            "subsections": []
        },
        "PUBLIC INTERFACE": {
            "content": "",
            "subsections": [
                {
                    "name": "Construction",
                    "content": "new [ARGS...]\n*Class method.* Return a new, unattached array handle. If any arguments are given, they're\nsent to open().\n\nopen [ARRAYREF]\n*Instance method.* Open the array handle on a new array, pointed to by ARRAYREF. If no\nARRAYREF is given, a \"private\" array is created to hold the file data.\n\nReturns the self object on success, undefined on error.\n\nopened\n*Instance method.* Is the array handle opened on something?\n\nclose\n*Instance method.* Disassociate the array handle from its underlying array. Done\nautomatically on destroy.\n"
                },
                {
                    "name": "Input and output",
                    "content": "flush\n*Instance method.* No-op, provided for OO compatibility.\n\nfileno\n*Instance method.* No-op, returns undef\n\ngetc\n*Instance method.* Return the next character, or undef if none remain. This does a read(1),\nwhich is somewhat costly.\n\ngetline\n*Instance method.* Return the next line, or undef on end of data. Can safely be called in an\narray context. Currently, lines are delimited by \"\\n\".\n\ngetlines\n*Instance method.* Get all remaining lines. It will croak() if accidentally called in a\nscalar context.\n\nprint ARGS...\n*Instance method.* Print ARGS to the underlying array.\n\nCurrently, this always causes a \"seek to the end of the array\" and generates a new array\nentry. This may change in the future.\n\nread BUF, NBYTES, [OFFSET];\n*Instance method.* Read some bytes from the array. Returns the number of bytes actually\nread, 0 on end-of-file, undef on error.\n\nwrite BUF, NBYTES, [OFFSET];\n*Instance method.* Write some bytes into the array.\n\nSeeking/telling and other attributes\nautoflush\n*Instance method.* No-op, provided for OO compatibility.\n\nbinmode\n*Instance method.* No-op, provided for OO compatibility.\n\nclearerr\n*Instance method.* Clear the error and EOF flags. A no-op.\n\neof *Instance method.* Are we at end of file?\n\nseek POS,WHENCE\n*Instance method.* Seek to a given position in the stream. Only a WHENCE of 0 (SEEKSET) is\nsupported.\n\ntell\n*Instance method.* Return the current position in the stream, as a numeric offset.\n\nsetpos POS\n*Instance method.* Seek to a given position in the array, using the opaque getpos() value.\nDon't expect this to be a number.\n\ngetpos\n*Instance method.* Return the current position in the array, as an opaque value. Don't\nexpect this to be a number.\n\naref\n*Instance method.* Return a reference to the underlying array.\n"
                }
            ]
        },
        "WARNINGS": {
            "content": "Perl's TIEHANDLE spec was incomplete prior to 5.00557; it was missing support for \"seek()\",\n\"tell()\", and \"eof()\". Attempting to use these functions with an IO::ScalarArray will not work\nprior to 5.00557. IO::ScalarArray will not have the relevant methods invoked; and even worse,\nthis kind of bug can lie dormant for a while. If you turn warnings on (via $^W or \"perl -w\"),\nand you see something like this...\n\nattempt to seek on unopened filehandle\n\n...then you are probably trying to use one of these functions on an IO::ScalarArray with an old\nPerl. The remedy is to simply use the OO version; e.g.:\n\n$AH->seek(0,0);    ### GOOD: will work on any 5.005\nseek($AH,0,0);     ### WARNING: will only work on 5.00557 and beyond\n",
            "subsections": []
        },
        "VERSION": {
            "content": "$Id: ScalarArray.pm,v 1.7 2005/02/10 21:21:53 dfs Exp $\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "",
            "subsections": [
                {
                    "name": "Primary Maintainer",
                    "content": "Dianne Skoll (dfs@roaringpenguin.com).\n"
                },
                {
                    "name": "Principal author",
                    "content": "Eryq (eryq@zeegee.com). President, ZeeGee Software Inc (http://www.zeegee.com).\n"
                },
                {
                    "name": "Other contributors",
                    "content": "Thanks to the following individuals for their invaluable contributions (if I've forgotten or\nmisspelled your name, please email me!):\n\n*Andy Glew,* for suggesting \"getc()\".\n\n*Brandon Browning,* for suggesting \"opened()\".\n\n*Eric L. Brine,* for his offset-using read() and write() implementations.\n\n*Doug Wilson,* for the IO::Handle inheritance and automatic tie-ing.\n"
                }
            ]
        }
    },
    "summary": "IO::ScalarArray - IO:: interface for reading/writing an array of scalars",
    "flags": [],
    "examples": [],
    "see_also": []
}