in reply to Re^4: Unable to rename the contents of the file using perl?
in thread Unable to rename the contents of the file using perl?
If you run this SSCCE then you see where one problem is
poj#!perl use strict; my $map = { one =>1, two =>2, three=>3, }; my ($regex) = map {qr /\b(?:$_)\b/ } join '|', map {quotemeta} keys %$ +map; change_file('test.txt',$map,$regex); sub change_file { my ( $fn, $map, $regex ) = @_; # print $fn; open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!"; my $str = do { local $/; <$fh> }; print $str; close $fh; my $num_replacements = $str =~ s/($regex)/$map->{$1}/ge; if ( $num_replacements ) { write_new_file( $fn, \$str ); } } sub write_new_file { my ( $fn, $str ) = @_; open ( my $fh, '>', $fn ) or die "Could not open file '$fn': $!"; print $fh $str; #<-- error here should be $$str close $fh; } < #test.txt __DATA__ one two three four five
|
|---|