可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a small text file that I'd like to read into a scalar variable exactly as it is in the file (preserving line separators and other whitespace).
The equivalent in Python would be something like
buffer = "" try: file = open("fileName", 'rU') try: buffer += file.read() finally: file.close() except IOError: buffer += "The file could not be opened."
This is for simply redisplaying the contents of the file on a web page, which is why my error message is going into my file buffer.
回答1:
From the Perl Cookbook:
my $filename = 'file.txt'; open( FILE, '<', $filename ) or die 'Could not open file: ' . $!; undef $/; my $whole_file = <FILE>;
I would localize the changes though:
my $whole_file = ''; { local $/; $whole_file = <FILE>; }
回答2:
As an alternative to what Alex said, you can install the File::Slurp module (cpan -i File::Slurp
from the command line) and use this:
use File::Slurp; # Read data into a variable my $buffer = read_file("fileName"); # or read data into an array my @buffer = read_file("fileName");
Note that this die
s (well... croak
s, but that's just the proper way to call die from a module) on errors, so you may need to run this in an eval block to catch any errors.
回答3:
If I don't have Slurp or Perl6::Slurp near by then I normally go with....
open my $fh, '<', 'file.txt' or die $!; my $whole_file = do { local $/; <$fh> };
回答4:
There is a discussion of the various ways to read a file here.
回答5:
I don't have enough reputation to comment, so I apologize for making this another post.
@ Harold Bamford: $/ should not be an obscure variable to a Perl programmer. A beginner may not know it, but he or she should learn it. The join method is a poor choice for the reasons stated in the article linked by hackingwords above. Here's the relevant quotation from the article:
That needlessly splits the input file into lines (join provides a list context to ) and then joins up those lines again. The original coder of this idiom obviously never read perlvar and learned how to use $/ to allow scalar slurping.
回答6:
You could do something like:
$data_file="somefile.txt"; open(DAT, $data_file); @file_data = <DAT>; close(DAT);
That'll give you the file contents in an array, that you can use for whatever you want, for example, if you wanted each individual line, you could do something like:
foreach $LINE (@file_data) { dosomethingwithline($LINE); }
For a full usage example:
my $result; $data_file = "somefile.txt"; my $opened = open(DAT, $data_file); if (!$opened) { $result = "Error."; } else { @lines = <DAT>; foreach $LINE (@lines) { $result .= $LINE; } close(DAT); }
Then you can use $result
however you need. Note: This code is untested, but it should give you an idea.
回答7:
I'd tweak draegtun's answer like this, to make it do exactly what was being asked:
my $buffer; if ( open my $fh, '<', 'fileName' ) { $buffer = do { local $/; <$fh> }; close $fh; } else { $buffer = 'The file could not be opened.'; }
回答8:
Just join all lines together into a string:
open(F, $file) or die $!; my $content = join("", <F>); close F;
(It was previously suggested to use join "\n" but that will add extra newlines. Each line already has a newline at its end when it's read.)