Reputation: 75
I'm trying to read a textfile into variable in perl, but it reads a textfile just until the first newline char.(enter at the end of the sentence) Here is my code:
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use v5.16;
open(FILE,"<:encoding(UTF-8)", 'data/text.txt') or die "Can't read file [$!]\n";
chomp(my $document = (<FILE>));
close (FILE);
Thanks for help!
Upvotes: 1
Views: 40
Reputation: 50637
Files are by default read line by line, and you have to change input record separator
variable $/
to undef
in order to get it into slurp mode,
my $document = do { local $/; <FILE> };
Upvotes: 2