{
    "mode": "perldoc",
    "parameter": "File::Slurp",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/File%3A%3ASlurp/json",
    "generated": "2026-06-12T16:07:06Z",
    "synopsis": "use File::Slurp;\n# read in a whole file into a scalar\nmy $text = readfile('/path/file');\n# read in a whole file into an array of lines\nmy @lines = readfile('/path/file');\n# write out a whole file from a scalar\nwritefile('/path/file', $text);\n# write out a whole file from an array of lines\nwritefile('/path/file', @lines);\n# Here is a simple and fast way to load and save a simple config file\n# made of key=value lines.\nmy %conf = readfile('/path/file') =~ /^(\\w+)=(.*)$/mg;\nwritefile('/path/file', {atomic => 1}, map \"$=$conf{$}\\n\", keys %conf);\n# insert text at the beginning of a file\nprependfile('/path/file', $text);\n# in-place edit to replace all 'foo' with 'bar' in file\neditfile { s/foo/bar/g } '/path/file';\n# in-place edit to delete all lines with 'foo' from file\neditfilelines sub { $ = '' if /foo/ }, '/path/file';\n# read in a whole directory of file names (skipping . and ..)\nmy @files = readdir('/path/to/dir');",
    "sections": {
        "NAME": {
            "content": "File::Slurp - Simple and Efficient Reading/Writing/Modifying of Complete Files\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use File::Slurp;\n\n# read in a whole file into a scalar\nmy $text = readfile('/path/file');\n\n# read in a whole file into an array of lines\nmy @lines = readfile('/path/file');\n\n# write out a whole file from a scalar\nwritefile('/path/file', $text);\n\n# write out a whole file from an array of lines\nwritefile('/path/file', @lines);\n\n# Here is a simple and fast way to load and save a simple config file\n# made of key=value lines.\nmy %conf = readfile('/path/file') =~ /^(\\w+)=(.*)$/mg;\nwritefile('/path/file', {atomic => 1}, map \"$=$conf{$}\\n\", keys %conf);\n\n# insert text at the beginning of a file\nprependfile('/path/file', $text);\n\n# in-place edit to replace all 'foo' with 'bar' in file\neditfile { s/foo/bar/g } '/path/file';\n\n# in-place edit to delete all lines with 'foo' from file\neditfilelines sub { $ = '' if /foo/ }, '/path/file';\n\n# read in a whole directory of file names (skipping . and ..)\nmy @files = readdir('/path/to/dir');\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module provides subs that allow you to read or write entire files with one simple call.\nThey are designed to be simple to use, have flexible ways to pass in or get the file contents\nand to be very efficient. There is also a sub to read in all the files in a directory.\n\nWARNING - PENDING DOOM\nAlthough you technically *can*, do NOT use this module to work on file handles, pipes, sockets,\nstandard IO, or the \"DATA\" handle. These are features implemented long ago that just really\nshouldn't be abused here.\n\nBe warned: this activity will lead to inaccurate encoding/decoding of data.\n\nAll further mentions of actions on the above have been removed from this documentation and that\nfeature set will likely be deprecated in the future.\n\nIn other words, if you don't have a filename to pass, consider using the standard \"do { local\n$/; <$fh> }\", or Data::Section/Data::Section::Simple for working with \"DATA\".\n",
            "subsections": []
        },
        "FUNCTIONS": {
            "content": "File::Slurp implements the following functions.\n\nappendfile\nuse File::Slurp qw(appendfile writefile);\nmy $res = appendfile('/path/file', \"Some text\");\n# same as\nmy $res = writefile('/path/file', {append => 1}, \"Some text\");\n\nThe \"appendfile\" function is simply a synonym for the \"writefile\" in File::Slurp function, but\nensures that the \"append\" option is set.\n\neditfile\nuse File::Slurp qw(editfile);\n# perl -0777 -pi -e 's/foo/bar/g' /path/file\neditfile { s/foo/bar/g } '/path/file';\neditfile sub { s/foo/bar/g }, '/path/file';\nsub replacefoo { s/foo/bar/g }\neditfile \\&replacefoo, '/path/file';\n\nThe \"editfile\" function reads in a file into $, executes a code block that should modify $,\nand then writes $ back to the file. The \"editfile\" function reads in the entire file and calls\nthe code block one time. It is equivalent to the \"-pi\" command line options of Perl but you can\ncall it from inside your program and not have to fork out a process.\n\nThe first argument to \"editfile\" is a code block or a code reference. The code block is not\nfollowed by a comma (as with \"grep\" and \"map\") but a code reference is followed by a comma.\n\nThe next argument is the filename.\n\nThe next argument(s) is either a hash reference or a flattened hash, \"key => value\" pairs. The\noptions are passed through to the \"writefile\" in File::Slurp function. All options are\ndescribed there. Only the \"binmode\" and \"errmode\" options are supported. The call to\n\"writefile\" in File::Slurp has the \"atomic\" option set so you will always have a consistent\nfile.\n\neditfilelines\nuse File::Slurp qw(editfilelines);\n# perl -pi -e '$ = \"\" if /foo/' /path/file\neditfilelines { $ = '' if /foo/ } '/path/file';\neditfilelines sub { $ = '' if /foo/ }, '/path/file';\nsub deletefoo { $ = '' if /foo/ }\neditfile \\&deletefoo, '/path/file';\n\nThe \"editfilelines\" function reads each line of a file into $, and executes a code block that\nshould modify $. It will then write $ back to the file. It is equivalent to the \"-pi\" command\nline options of Perl but you can call it from inside your program and not have to fork out a\nprocess.\n\nThe first argument to \"editfilelines\" is a code block or a code reference. The code block is\nnot followed by a comma (as with \"grep\" and \"map\") but a code reference is followed by a comma.\n\nThe next argument is the filename.\n\nThe next argument(s) is either a hash reference or a flattened hash, \"key => value\" pairs. The\noptions are passed through to the \"writefile\" in File::Slurp function. All options are\ndescribed there. Only the \"binmode\" and \"errmode\" options are supported. The call to\n\"writefile\" in File::Slurp has the \"atomic\" option set so you will always have a consistent\nfile.\n\nef\nuse File::Slurp qw(ef);\n# perl -0777 -pi -e 's/foo/bar/g' /path/file\nef { s/foo/bar/g } '/path/file';\nef sub { s/foo/bar/g }, '/path/file';\nsub replacefoo { s/foo/bar/g }\nef \\&replacefoo, '/path/file';\n\nThe \"ef\" function is simply a synonym for the \"editfile\" in File::Slurp function.\n\nefl\nuse File::Slurp qw(efl);\n# perl -pi -e '$ = \"\" if /foo/' /path/file\nefl { $ = '' if /foo/ } '/path/file';\nefl sub { $ = '' if /foo/ }, '/path/file';\nsub deletefoo { $ = '' if /foo/ }\nefl \\&deletefoo, '/path/file';\n\nThe \"efl\" function is simply a synonym for the \"editfilelines\" in File::Slurp function.\n\noverwritefile\nuse File::Slurp qw(overwritefile);\nmy $res = overwritefile('/path/file', \"Some text\");\n\nThe \"overwritefile\" function is simply a synonym for the \"writefile\" in File::Slurp function.\n\nprependfile\nuse File::Slurp qw(prependfile);\nprependfile('/path/file', $header);\nprependfile('/path/file', \\@lines);\nprependfile('/path/file', { binmode => ':raw'}, $bindata);\n\n# equivalent to:\nuse File::Slurp qw(readfile writefile);\nmy $content = readfile('/path/file');\nmy $newcontent = \"hahahaha\";\nwritefile('/path/file', $newcontent . $content);\n\nThe \"prependfile\" function is the opposite of \"appendfile\" in File::Slurp as it writes new\ncontents to the beginning of the file instead of the end. It is a combination of \"readfile\" in\nFile::Slurp and \"writefile\" in File::Slurp. It works by first using \"readfile\" to slurp in the\nfile and then calling \"writefile\" with the new data and the existing file data.\n\nThe first argument to \"prependfile\" is the filename.\n\nThe next argument(s) is either a hash reference or a flattened hash, \"key => value\" pairs. The\noptions are passed through to the \"writefile\" in File::Slurp function. All options are\ndescribed there.\n\nOnly the \"binmode\" and \"errmode\" options are supported. The \"writefile\" call has the \"atomic\"\noption set so you will always have a consistent file.\n\nreaddir\nuse File::Slurp qw(readdir);\nmy @files = readdir('/path/to/dir');\n# all files, even the dots\nmy @files = readdir('/path/to/dir', keepdotdot => 1);\n# keep the full file path\nmy @paths = readdir('/path/to/dir', prefix => 1);\n# scalar context\nmy $filesref = readdir('/path/to/dir');\n\nThis function returns a list of the filenames in the supplied directory. In list context, an\narray is returned, in scalar context, an array reference is returned.\n\nThe first argument is the path to the directory to read.\n\nThe next argument(s) is either a hash reference or a flattened hash, \"key => value\" pairs. The\nfollowing options are available:\n\n*   errmode\n\nThe \"errmode\" option has three possible values: \"quiet\", \"carp\", or the default, \"croak\".\nIn \"quiet\" mode, all errors will be silent. In \"carp\" mode, all errors will be emitted as\nwarnings. And, in \"croak\" mode, all errors will be emitted as exceptions. Take a look at\nTry::Tiny or Syntax::Keyword::Try to see how to catch exceptions.\n\n*   keepdotdot\n\nThe \"keepdotdot\" option is a boolean option, defaulted to false (0). Setting this option\nto true (1) will also return the \".\" and \"..\" files that are removed from the file list by\ndefault.\n\n*   prefix\n\nThe \"prefix\" option is a boolean option, defaulted to false (0). Setting this option to true\n(1) add the directory as a prefix to the file. The directory and the filename are joined\nusing \"File::Spec->catfile()\" to ensure the proper directory separator is used for your OS.\nSee File::Spec.\n\nreadfile\nuse File::Slurp qw(readfile);\nmy $text = readfile('/path/file');\nmy $bin = readfile('/path/file', { binmode => ':raw' });\nmy @lines = readfile('/path/file');\nmy $linesref = readfile('/path/file', arrayref => 1);\nmy $linesref = [ readfile('/path/file') ];\n\n# or we can read into a buffer:\nmy $buffer;\nreadfile('/path/file', bufref => \\$buffer);\n\n# or we can set the block size for the read\nmy $textref = readfile('/path/file', blksize => 10000000, arrayref => 1);\n\n# or we can get a scalar reference\nmy $textref = readfile('/path/file', scalarref => 1);\n\nThis function reads in an entire file and returns its contents to the caller. In scalar context\nit returns the entire file as a single scalar. In list context it will return a list of lines\n(using the current value of $/ as the separator, including support for paragraph mode when it is\nset to '').\n\nThe first argument is the path to the file to be slurped in.\n\nThe next argument(s) is either a hash reference or a flattened hash, \"key => value\" pairs. The\nfollowing options are available:\n\n*   arrayref\n\nThe \"arrayref\" option is a boolean option, defaulted to false (0). Setting this option to\ntrue (1) will only have relevance if the \"readfile\" function is called in scalar context.\nWhen true, the \"readfile\" function will return a reference to an array of the lines in the\nfile.\n\n*   binmode\n\nThe \"binmode\" option is a string option, defaulted to empty (''). If you set the \"binmode\"\noption, then its value is passed to a call to \"binmode\" on the opened handle. You can use\nthis to set the file to be read in binary mode, utf8, etc. See \"perldoc -f binmode\" for\nmore.\n\nPlease note that using binmode :utf8 with sysread (and thus readfile) has been deprecated\nin recent versions of perl.\n\n*   blksize\n\nYou can use this option to set the block size used when slurping from an already open handle\n(like \"\\*STDIN\"). It defaults to 1MB.\n\n*   bufref\n\nThe \"bufref\" option can be used in conjunction with any of the other options. You can use\nthis option to pass in a scalar reference and the slurped file contents will be stored in\nthe scalar. This saves an extra copy of the slurped file and can lower RAM usage vs\nreturning the file. It is usually the fastest way to read a file into a scalar.\n\n*   chomp\n\nThe \"chomp\" option is a boolean option, defaulted to false (0). Setting this option to true\n(1) will cause each line to have its contents \"chomp\"ed. This option works in list context\nor in scalar context with the \"arrayref\" option.\n\n*   errmode\n\nThe \"errmode\" option has three possible values: \"quiet\", \"carp\", or the default, \"croak\".\nIn \"quiet\" mode, all errors will be silent. In \"carp\" mode, all errors will be emitted as\nwarnings. And, in \"croak\" mode, all errors will be emitted as exceptions. Take a look at\nTry::Tiny or Syntax::Keyword::Try to see how to catch exceptions.\n\n*   scalarref\n\nThe \"scalarref\" option is a boolean option, defaulted to false (0). It only has meaning in\nscalar context. The return value will be a scalar reference to a string which is the\ncontents of the slurped file. This will usually be faster than returning the plain scalar.\nIt will also save memory as it will not make a copy of the file to return.\n\nrf\nuse File::Slurp qw(rf);\nmy $text = rf('/path/file');\n\nThe \"rf\" function is simply a synonym for the \"readfile\" in File::Slurp function.\n\nslurp\nuse File::Slurp qw(slurp);\nmy $text = slurp('/path/file');\n\nThe \"slurp\" function is simply a synonym for the \"readfile\" in File::Slurp function.\n\nwf\nuse File::Slurp qw(wf);\nmy $res = wf('/path/file', \"Some text\");\n\nThe \"wf\" function is simply a synonym for the \"writefile\" in File::Slurp function.\n\nwritefile\nuse File::Slurp qw(writefile);\nwritefile('/path/file', @data);\nwritefile('/path/file', {append => 1}, @data);\nwritefile('/path/file', {binmode => ':raw'}, $buffer);\nwritefile('/path/file', \\$buffer);\nwritefile('/path/file', $buffer);\nwritefile('/path/file', \\@lines);\nwritefile('/path/file', @lines);\n\n# binmode\nwritefile('/path/file', {binmode => ':raw'}, @data);\nwritefile('/path/file', {binmode => ':utf8'}, $utftext);\n\n# buffered\nwritefile('/path/file', {bufref => \\$buffer});\nwritefile('/path/file', \\$buffer);\nwritefile('/path/file', $buffer);\n\n# append\nwritefile('/path/file', {append => 1}, @data);\n\n# no clobbering\nwritefile('/path/file', {noclobber => 1}, @data);\n\nThis function writes out an entire file in one call. By default \"writefile\" returns 1 upon\nsuccessfully writing the file or \"undef\" if it encountered an error. You can change how errors\nare handled with the \"errmode\" option.\n\nThe first argument to \"writefile\" is the filename.\n\nThe next argument(s) is either a hash reference or a flattened hash, \"key => value\" pairs. The\nfollowing options are available:\n\n*   append\n\nThe \"append\" option is a boolean option, defaulted to false (0). Setting this option to true\n(1) will cause the data to be be written at the end of the current file. Internally this\nsets the \"sysopen\" mode flag \"OAPPEND\".\n\nThe \"appendfile\" in File::Slurp function sets this option by default.\n\n*   atomic\n\nThe \"atomic\" option is a boolean option, defaulted to false (0). Setting this option to true\n(1) will cause the file to be be written to in an atomic fashion. A temporary file name is\ncreated using \"tempfile\" in File::Temp. After the file is closed it is renamed to the\noriginal file name (and \"rename\" is an atomic operation on most OSes). If the program using\nthis were to crash in the middle of this, then the temporary file could be left behind.\n\n*   binmode\n\nThe \"binmode\" option is a string option, defaulted to empty (''). If you set the \"binmode\"\noption, then its value is passed to a call to \"binmode\" on the opened handle. You can use\nthis to set the file to be read in binary mode, utf8, etc. See \"perldoc -f binmode\" for\nmore.\n\n*   bufref\n\nThe \"bufref\" option is used to pass in a scalar reference which has the data to be written.\nIf this is set then any data arguments (including the scalar reference shortcut) in @ will\nbe ignored.\n\n*   errmode\n\nThe \"errmode\" option has three possible values: \"quiet\", \"carp\", or the default, \"croak\".\nIn \"quiet\" mode, all errors will be silent. In \"carp\" mode, all errors will be emitted as\nwarnings. And, in \"croak\" mode, all errors will be emitted as exceptions. Take a look at\nTry::Tiny or Syntax::Keyword::Try to see how to catch exceptions.\n\n*   noclobber\n\nThe \"noclobber\" option is a boolean option, defaulted to false (0). Setting this option to\ntrue (1) will ensure an that existing file will not be overwritten.\n\n*   perms\n\nThe \"perms\" option sets the permissions of newly-created files. This value is modified by\nyour process's \"umask\" and defaults to 0666 (same as \"sysopen\").\n\nNOTE: this option is new as of File::Slurp version 9999.14.\n",
            "subsections": []
        },
        "EXPORT": {
            "content": "These are exported by default or with\n\nuse File::Slurp qw(:std);\n# readfile writefile overwritefile appendfile readdir\n\nThese are exported with\n\nuse File::Slurp qw(:edit);\n# editfile editfilelines\n\nYou can get all subs in the module exported with\n\nuse File::Slurp qw(:all);\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "*   File::Slurper - Provides a straightforward set of functions for the most common tasks of\nreading/writing text and binary files.\n\n*   Path::Tiny - Lightweight and comprehensive file handling, including simple methods for\nreading, writing, and editing text and binary files.\n\n*   Mojo::File - Similar to Path::Tiny for the Mojo toolkit, always works in bytes.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Uri Guttman, <uri@stemsystems.com>\n\nCOPYRIGHT & LICENSE\nCopyright (c) 2003 Uri Guttman. All rights reserved.\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n",
            "subsections": []
        }
    },
    "summary": "File::Slurp - Simple and Efficient Reading/Writing/Modifying of Complete Files",
    "flags": [],
    "examples": [],
    "see_also": []
}