PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

I need to write a script that opens a file and either:

1) deletes the last line and replaces it. or alternatively.

2) compares the last line and if it is not equal to what I need it to be then it replaces it. I think option 1 will be simpler, so I'm going with that. However, the line that I need to inset must have the file name in it. ie: File name ABCD Replaced Line: <xxxx.xxx.xxx.xxx, "xxx", xx_ABCD, xxx="">

so I open my file by:
#Takes file name from command prompt #Then assigns the name to str fileName $fileName = INFILE; #open the file and copy to an array open(INFILE) || die("Cannot Open File"); @aFile = <INFILE>; close(INFILE); # How do I get the script to continue if line 6 is # already deleted? # Delete line 6 splice(@aFile,5) || die("Line 6 is already deleted");

This is where I am stuck. I need to append the end of the file with a long string (see example above) that includes the file name. How difficult would it be to implement a test of the last string and output if the change had been made or not? Any assistance would be greatly appreciated. Thanks in advance

Replies are listed 'Best First'.
Re: Find and Replace
by kiruthika.bkite (Scribe) on Mar 25, 2010 at 03:53 UTC
    use strict; use warnings; use Data::Dumper; use Tie::File; my @array; tie @array,'Tie::File',$ARGV[0]; $array[-1]= qq(<xxxx.xxx.xxx.xxx, "xxx", xx_$ARGV[0], xxx="">); print Dumper(\@array);
Re: Find and Replace
by zwon (Abbot) on Mar 24, 2010 at 21:17 UTC

    Didn't actually get what do you want, but here's the code that assigns new value to line 6:

    use strict; use warnings; my $fileName = shift; #open the file and copy to an array open my $fh, "<", $fileName or die "Cannot Open File: $!"; my @aFile = <$fh>; close $fh; # use $aFile[-1] for the last line, and $aFile[5] for the 6th line $aFile[5] = qq(<xxxx.xxx.xxx.xxx, "xxx", xx_$fileName, xxx="">);
Re: Find and Replace
by toolic (Bishop) on Mar 24, 2010 at 21:21 UTC
    Since you are already treating your file like an array of lines, you might be interested in Tie::File.

    Also, your code has major problems: use strict and warnings. Compiling your code generates warnings:

    perl -wc -Mdiagnostics 830674.pl
    This generates errors:
    perl -wc -Mdiagnostics -Mstrict 830674.pl
Re: Find and Replace
by Anonymous Monk on Mar 24, 2010 at 21:09 UTC
Re: Find and Replace
by PyrexKidd (Monk) on Mar 25, 2010 at 16:03 UTC

    so here's what I've come up with. any suggestions for debugging this script?

    use strict; use warnings; use Fcntl qw(:flock :seek); my($fileName) = $ARGV[0]; print("$fileName \n"); #open the file and copy to an array open(INFILE, $ARGV[0]) || die("Cannot Open File"); my(@aFile) = <INFILE>; close(INFILE); # Delete line 6 splice(@aFile,5) || die("Line 6 is already deleted"); open(OUTFILE,">$fileName") || die("Cannot Open File"); flock(OUTFILE, LOCK_EX); seek(OUTFILE, 0, SEEK_SET); print OUTFILE @aFile; print OUTFILE "<APPLICATION APP_FILE_PATH=\"xxxxxx" CONFIG_FILES=\"xxx +x/$fileName, MISC_FILES=\"\" LOG_FILE_DIRECTORY=\"\" OVERRIDES_DIRECT +ORY=\"\" CONTACTS_DIRECTORY=\"\" LICENSE_DIRECTORY=\"\"/>"; close(OUTFILE);

      It looks like you are working with an XML file so you should probably be using something like XML::Twig or XML::TreeBuilder to manipulate the data.

      Aside from that, the first thing to do is get your code passing strictures. As presented there is an unquoted " that generates an error. Using qq instead of a bare " quoted string would help a lot. eg:

      print OUTFILE qq~<APPLICATION APP_FILE_PATH="xxxxxx" CONFIG_FILES="xxx +x...~;

      True laziness is hard work