{
    "mode": "perldoc",
    "parameter": "Data::Buffer",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Data%3A%3ABuffer/json",
    "generated": "2026-06-10T16:25:24Z",
    "synopsis": "use Data::Buffer;\nmy $buffer = Data::Buffer->new;\n## Add a 32-bit integer.\n$buffer->putint32(10932930);\n## Get it back.\nmy $int = $buffer->getint32;",
    "sections": {
        "NAME": {
            "content": "Data::Buffer - Read/write buffer class\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Data::Buffer;\nmy $buffer = Data::Buffer->new;\n\n## Add a 32-bit integer.\n$buffer->putint32(10932930);\n\n## Get it back.\nmy $int = $buffer->getint32;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "*Data::Buffer* implements a low-level binary buffer in which you can get and put integers,\nstrings, and other data. Internally the implementation is based on \"pack\" and \"unpack\", such\nthat *Data::Buffer* is really a layer on top of those built-in functions.\n\nAll of the *get and *put methods respect the internal offset state in the buffer object.\nThis means that you should read data out of the buffer in the same order that you put it in. For\nexample:\n\n$buf->putint16(24);\n$buf->putint32(1233455);\n$buf->putint16(99);\n\n$buf->getint16;   # 24\n$buf->getint32;   # 1233455\n$buf->getint16;   # 99\n\nOf course, this assumes that you *know* the order of the data items in the buffer. If your setup\nis such that your sending and receiving processes won't necessarily know what's inside the\nbuffers they receive, take a look at the *TEMPLATE USAGE* section.\n",
            "subsections": []
        },
        "USAGE": {
            "content": "Data::Buffer->new\nCreates a new buffer object and returns it. The buffer is initially empty.\n\nThis method takes no arguments.\n\nData::Buffer->newwithinit(@strs)\nCreates a new buffer object and appends to it each of the octet strings in *@strs*.\n\nReturns the new buffer object.\n\n$buffer->getint8\nReturns the next 8-bit integer from the buffer (which is really just the ASCII code for the next\ncharacter/byte in the buffer).\n\n$buffer->putint8\nAppends an 8-bit integer to the buffer (which is really just the character corresponding to that\ninteger, in ASCII).\n\n$buffer->getint16\nReturns the next 16-bit integer from the buffer.\n\n$buffer->putint16($integer)\nAppends a 16-bit integer to the buffer.\n\n$buffer->getint32\nReturns the next 32-bit integer from the buffer.\n\n$buffer->putint32($integer)\nAppends a 32-bit integer to the buffer.\n\n$buffer->getchar\nMore appropriately called *getbyte*, perhaps, this returns the next byte from the buffer.\n\n$buffer->putchar($bytes)\nAppends a byte (or a sequence of bytes) to the buffer. There is no restriction on the length of\nthe byte string *$bytes*; if it makes you uncomfortable to call *putchar* to put multiple\nbytes, you can instead call this method as *putchars*. It's the same thing.\n\n$buffer->getbytes($n)\nGrabs *$n* bytes from the buffer, where *$n* is a positive integer. Increments the internal\noffset state by *$n*.\n\n$buffer->putbytes($bytes [, $n ])\nAppends a sequence of bytes to the buffer; if *$n* is unspecified, appends the entire length of\n*$bytes*. Otherwise appends only the first *$n* bytes of *$bytes*.\n\n$buffer->getstr\nReturns the next \"string\" from the buffer. A string here is represented as the length of the\nstring (a 32-bit integer) followed by the string itself.\n\n$buffer->putstr($string)\nAppends a string (32-bit integer length and the string itself) to the buffer.\n\n$buffer->extract($n)\nExtracts the next *$n* bytes from the buffer *$buffer*, increments the offset state in\n*$buffer*, and returns a new buffer object containing the extracted bytes.\n",
            "subsections": []
        },
        "TEMPLATE USAGE": {
            "content": "Generally when you use *Data::Buffer* it's to communicate with another process (perhaps a C\nprogram) that bundles up its data into binary buffers. In those cases, it's very likely that the\ndata will be in some well-known order in the buffer: in other words, it might be documented that\na certain C program creates a buffer containing:\n\n*   an int8\n\n*   a string\n\n*   an int32\n\nIn this case, you would presumably know about the order of the data in the buffer, and you could\nextract it accordingly:\n\n$buffer->getint8;\n$buffer->getstr;\n$buffer->getint32;\n\nIn other cases, however, there may not be a well-defined order of data items in the buffer. This\nmight be the case if you're inventing your own protocol, and you want your binary buffers to\n\"know\" about their contents. In this case, you'll want to use the templating features of\n*Data::Buffer*.\n\nWhen you use the *put* methods to place data in a buffer, *Data::Buffer* keeps track of the\ntypes of data that you're inserting in a template description of the buffer. This template\ncontains all of the information necessary for a process to receive a buffer and extract the data\nin the buffer without knowledge of the order of the items.\n\nTo use this feature, simply use the *inserttemplate* method after you've filled your buffer to\ncompletion. For example:\n\nmy $buffer = Data::Buffer->new;\n$buffer->putstr(\"foo\");\n$buffer->putint32(9999);\n$buffer->inserttemplate;\n\n## Ship off the buffer to another process.\n\nThe receiving process should then invoke the *getall* method on the buffer to extract all of\nthe data:\n\nmy $buffer = Data::Buffer->new;\n$buffer->append( $receivedbufferdata );\nmy @data = $buffer->getall;\n\n@data will now contain two elements: \"foo\" and 9999.\n",
            "subsections": []
        },
        "LOW-LEVEL METHODS": {
            "content": "$buffer->append($bytes)\nAppends raw data *$bytes* to the end of the in-memory buffer. Generally you don't need to use\nthis method unless you're initializing an empty buffer, because when you need to add data to a\nbuffer you should generally use one of the *put methods.\n\n$buffer->empty\nEmpties out the buffer object.\n\n$buffer->bytes([ $offset [, $length [, $replacement ]]])\nBehaves exactly like the *substr* built-in function, except on the buffer *$buffer*. Given no\narguments, *bytes* returns the entire buffer; given one argument *$offset*, returns everything\nfrom that position to the end of the string; given *$offset* and *$length*, returns the segment\nof the buffer starting at *$offset* and consisting of *$length* bytes; and given all three\narguments, replaces that segment with *$replacement*.\n\nThis is a very low-level method, and you generally won't need to use it.\n\nAlso be warned that you should not intermix use of this method with use of the *get and\n*put methods; the latter classes of methods maintain internal state of the buffer offset\nwhere arguments will be gotten from and put, respectively. The *bytes* method gives no thought\nto this internal offset state.\n\n$buffer->length\nReturns the length of the buffer object.\n\n$buffer->offset\nReturns the internal offset state.\n\nIf you insist on intermixing calls to *bytes* with calls to the *get and *put methods,\nyou'll probably want to use this method to get some status on that internal offset.\n\n$buffer->setoffset($offset)\nSets the internal offset state to *$offset*.\n\n$buffer->resetoffset\nSets the internal offset state to 0.\n\n$buffer->dump(@args)\nReturns a hex dump of the buffer. The dump is of the *entire* buffer *$buffer*; in other words,\n*dump* doesn't respect the internal offset pointer.\n\n*@args* is passed directly through to the *bytes* method, which means that you can supply\narguments to emulate support of the internal offset:\n\nmy $dump = $buffer->dump($buffer->offset);\n\n$buffer->insertpadding\nA helper method: pads out the buffer so that the length of the transferred packet will be evenly\ndivisible by 8, which is a requirement of the SSH protocol.\n\nAUTHOR & COPYRIGHTS\nBenjamin Trott, ben@rhumba.pair.com\n\nExcept where otherwise noted, Data::Buffer is Copyright 2001 Benjamin Trott. All rights\nreserved. Data::Buffer is free software; you may redistribute it and/or modify it under the same\nterms as Perl itself.\n",
            "subsections": []
        }
    },
    "summary": "Data::Buffer - Read/write buffer class",
    "flags": [],
    "examples": [],
    "see_also": []
}