File: get_an_open_file_handle.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 (28 lines) | stat: -rw-r--r-- 531 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
# ABSTRACT: Get an open file handle for reading or writing

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

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

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

# open the file for writing
my $fh = $ftl->open_handle( file => $file );

print $fh 'Hello World!';

close $fh; # <-- the file won't be unlocked in this process unless we close it

# open the file for reading now
$fh = $ftl->open_handle( file => $file, mode => 'read' );

while ( <$fh> ) {

   print;
}

close $fh;

exit;