File: load_a_file_into_a_variable.pl

package info (click to toggle)
libfile-util-perl 4.201720-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 840 kB
  • sloc: perl: 4,353; makefile: 2
file content (23 lines) | stat: -rw-r--r-- 567 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ABSTRACT: Load the contents of a file into a string or array

use strict;
use warnings;
use File::Util qw( NL );

my $ftl = File::Util->new();

my $file = 'example.txt'; # in this example, this file must already exist

# get the whole file in one string
my $content = $ftl->load_file( $file );

print $content;

# get the file in a list of lines
my @content_lines = $ftl->load_file( $file, '--as-lines' );

# The NL constant below will be the apropriate newline character sequence
# for your operating system... "\n" or "\r\n"
print join NL, @content_lines;

exit;