in reply to text file search and replace
- Put these two lines at the top of your program:
use warnings;
use strict;
- open SLURP, $comptmplt_file or die "sudden death";
Since you are using braces to limit the scope of local $/; you should probably also use a locally scoped filehandle. You should include the $! variable in the error message so you know why open failed.
- $COMPFILE->open("+< comp_no_rods.txt" or die "Can't open");
Your die statement will never execute because the string "+< comp_no_rods.txt" is always true. You need to move the closing parenthesis before or die. You should include the $! variable in the error message so you know why open failed.
- print "$comptmplt";
What's wrong with always quoting "$vars"?
- until ($COMPFILE -> eof){
$compline = $COMPFILE->getline();
In Perl you almost never need to use eof. That is usually written as:
while ( my $compline = <$COMPFILE> ) {
- if ($compline=~ /END/){;
$compline=~ s/END/$comptmplt/;
There is no need to use the same regular expression twice, just once is enough:
if ( $compline =~ s/END/$comptmplt/ ) {;
- Your main problem is that when you open a file in read-write mode ("+<") and read a line the file pointer moves to the end of that line and anything printed at that point overwrites any data in the file. If you want to edit the file "in place" you can use the in-place edit variable $^I:
( $^I, @ARGV ) = ( '.bak', 'comp_no_rods.txt' );
while ( <> ) {
s/END/$comptmplt/;
print;
}