#!/usr/bin/env perl use strict; use warnings; use autodie qw{:all}; use Tie::File; my $target_in = './pm_1073422_test.txt'; my $target_out = './pm_1073422_test.out'; print "*** Using Disk File ***\n"; { open my $fh_out, '>', $target_out; emulate_ctx_cat($fh_out, $target_in); close $fh_out; system cat => $target_out; unlink $target_out; } print "*** Using Array ***\n"; { open my $fh_out, '+>', undef; emulate_ctx_cat($fh_out, $target_in); tie my @text, 'Tie::File', $fh_out; print "* Original Text *\n"; print "$_\n" for @text; $text[2] = 'Modified line'; print "* Modified Text *\n"; print "$_\n" for @text; untie @text; close $fh_out; print "* Original File *\n"; system cat => $target_in; } sub emulate_ctx_cat { my ($fh_out, $target) = @_; open my $fh_in, '<', $target; while (<$fh_in>) { print $fh_out $_; } close $fh_in; }