---
title: "Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };"
author: "standalone"
date: "2012-12-28T16:25:47Z"
tags: []
categories: ["编程语言"]
slug: "perl-idioms-explained-my-string-do-local-filehandle"
draft: true
lastmod: "2012-12-28T16:25:47Z"
id: 1756540
url: "https://standalone.iteye.com/blog/1756540"
---

<http://www.perlmonks.org/index.pl?node_id=287647>

open FILEHANDLE, 'somefile.txt' or die $!;
my $string = do { local $/; <FILEHANDLE> };

The above idiom is a consise way to "slurp" the entire contents of a file into a scalar without using a loop, such as:

open FILEHANDLE, 'somefile.txt' or die $!;
my $string = '';
while (<FILEHANDLE>) {
    $string .= $_;
}