File: write_or_append_to_a_file.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 (30 lines) | stat: -rw-r--r-- 730 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
24
25
26
27
28
29
30
# ABSTRACT: Easily write or append to a file in one go

use strict;
use warnings;
use File::Util;

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

my $file = 'example.txt';

# writing content to the file, creating it if it doesn't exist
$ftl->write_file( file => $file, content => 'Hello World!' );

# you optionally specify a bitmask for a file if it doesn't exist yet.
# the bitmask is combined with the user's current umask for the creation
# mode of the file.  (You should usually omit this.)
$ftl->write_file(
   file    => 'new.txt',
   bitmask => oct 777,
   content => 'Hello World!'
);

# append to the file you just created
$ftl->write_file(
   file    => 'new.txt',
   content => 'Goodbye cruel world',
   mode    => 'append'
);

exit;