#!/usr/bin/env perl use strict; use warnings; use 5.010_001; use English qw( -no_match_vars ); # avoid regex performance penalty use File::Copy; use File::Temp qw(tempfile); use Readonly; Readonly my $STAT_INODE => '1'; Readonly my $STAT_SIZE => '7'; my $tmp_fh = fetch(); store($tmp_fh); exit 0; sub fetch { my $src_file = '/etc/services'; show_filestats('FETCH', $src_file); my ( $tmp1_fh, $tmp1_filename ) = tempfile(); unlink $tmp1_filename; # copy $src_file, $tmp1_fh or die "$ERRNO\n"; show_filestats('FETCH', $tmp1_fh); return $tmp1_fh; } sub store { my $tmp1_fh = shift; my $tmp2_file = File::Temp->new( TEMPLATE => '/tmp/services.XXXX', UNLINK => 0 ); my $dst_file = '/tmp/services_copy'; show_filestats('STORE', $tmp1_fh); show_filestats('STORE', $tmp2_file); print "[STORE] Copying filehandle tmp1 ($tmp1_fh) to tmp2 ($tmp2_file) file.\n" or croak(); copy $tmp1_fh, $tmp2_file or die "E: $ERRNO\n"; show_filestats('STORE', $tmp1_fh); show_filestats('STORE', $tmp2_file); # apply permissions and ACLs rename $tmp2_file, $dst_file or die "E: $ERRNO\n"; } sub show_filestats { my $id = shift; my $fh = shift; my ( $inode, $size ) = ( stat $fh )[ $STAT_INODE, $STAT_SIZE ]; print "[$id] The file '$fh' has inode $inode and size $size\n" or croak(); return; }