perl pra am Samstag, 25. November 2006 13:40:
> hi Gurus,
>
> I have a problem to replace strings of file thru perl script.
[...]
> I have a text file  some thing like this..
> PROJ_FOLER=C:\Proj
> PROJ_LOGS=C:\PROJ\LOGS
>
> I have same line in config file some thing like this.
> PROJ_FOLDER=D:\Proj
> PROJ_LOGS=D:\PROJ\LOGS.
[...]
> Here is the code i have written...

Hi perl pra

I did not test your code, and won't present a solution, because I think it is 
more helpful to make you think about what you coded, and how you can simplify 
coding, and then you will find the solution yourself :-)

> #!/usr/bin/perl

Never forget:

  use strict;
  use warnings;

you have to declare all variables now, and will see warnings and hints about 
possible error sources.

> my $config_path="E:/MessageArchive/WorkArea/config.txt";
> $file="E:/temp/FT/config/FTMessageArchive.configd";
> open FH, "$config_path";

Replace the couble qoutes with single qoutes in the first two lines, no 
variable is interpolated; in the third, a variable without any static text is 
unnecessarily interpolated, so you can omit quoting at all.

Note the usage of FH and $LOGFILE as file handles. In newer style, use a 
variable:

  open my $fh, '<', $config_path or die $!;

>   while ($line=<FH>) {
>       my ($key,$val)= $line =~ /^(\w+)=(.+)$/mg ;

The test if the matching succeeded is missing. Your data may be proper 
formatted or not. If not, $key and $val will be undefined, leading to 
unexpected results in the following code. Never assume properly formatted 
input data.

You only read one line, so the /m modifier is useless.

A data line, I assume, should only have one 'X=Y', so the /g modifier is a bit 
strange. You may want to handle unexpected data lines in some way.

>       $repline="$key=$val";

You break $line into parts and then put it together in $repline again. I don't 
see at the moment what's the sense behind it?!

>       open $LOGFILE, '<', $file;

*Always* check:

  open $LOGFILE, '<', $file or die $!; # even better: more verbose msg

>       while ($line1 = <$LOGFILE> ) {
>            if ($line1 =~ m/$key/){ 
>              system("perl -i.bak -p -e 's/$line1/$repline/g' $file");

*Always* check. system returns 0 on success. 

Then, the error messages you get, indicate that the code given to system 
contains some errors. What to do? Simply print out it to see what you pass to 
system. Eventually run the code directly in the shell.

You deal with user provided input here that is passed to the shell. So be 
*very* cautious about what is executed, and consider malicious input data.

Also note that you are in a while loop stepping through $file, and you try to 
modify $file via system. Generally it's a bad idea to alter something you're 
looping through.

>              close $LOGFILE;

*Always* check.

>              last; 
>            }  
>       }
> }
>
> close(FH);

*Always check.

> END _____________
>
> if i run the script I am getting the following errror..
>
>
> Can't find string terminator "'" anywhere before EOF at -e line 1.
> Can't find string terminator "'" anywhere before EOF at -e line 1.
> Can't find string terminator "'" anywhere before EOF at -e line 1.
[...]
> What am i doing wrong?

The most important point I think is that you should check as much as possible 
and assume as less as possible :-)

Then, as a next step, you may consider a redesign. Consider (poor pseudocode):

while ...{
   while ...{
     system...
   }
}

I don't know how many lines your files contain, but system could be executed 
numerous times, every time creating a new process!



Dani (nonguru)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to