By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,775 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,775 IT Pros & Developers. It's quick & easy.

Need help in Perl code..

P: 1
Hi All,

I'm a newbie in perl, have to do some patch work on my SDK so thaught perl can help me in doing this.

My requirement is as under:
---------------------------
root/
|--directory1---a.txt
|
|
|--directory2---b.txt


[ Hope this representation is clear!]

1. I've to insert some lines say "MY_COMMENT_HERE" in a.txt and b.txt files. This line is to be placed in between the code (not at start or end).

2. These lines shud be added to the code once only. If i re-run the script I shud receive an error message.

What I'm able to do:
--------------------
1. I've succedded in opening a file say "a.txt" and append my comment at the last (end of file). but this not a requirement.. actually it should append in between the code.

2. I am able to get the error message once I re-run my script. i.e. file is updated once only and not again and again..

Issues:
-------
1. I'm clueless about how to traverse across different folders in "root" directory and locate "a.txt" and then append the same...

2. How do I append in between the code..? I'm able to append at the end, but thats not required...

My code is as under:
place all code in
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #!C:/Perl/bin/perl.exe -w  
  4.  
  5. $My_File="load_env.sh"; 
  6. $Target = " YOUR_COMMENT_HERE "; 
  7.  
  8. if ( !is_target_found( $My_File, $Target ) ) 
  9. {     open($My_File_FP,">>", "$My_File") || die("$MyFilewill not open for update!");     
  10. print $My_File_FP "\n  $Target \n";     
  11. close($My_File_FP); }    
  12.  
  13. sub is_target_found 
  14. { (my $My_File, my $Target) = @_ ;  
  15.    open(my $My_File_FP, "<", "$My_File") || die "$MyFile will not open for input!";
  16.      my $is_found = 0; 
  17.      TARGET_LOOP:
  18.      while ( <$My_File_FP> )
  19.      {        
  20.      if ( /$Target/ )  
  21.          {  
  22.              print " found $Target on line $. \n";
  23.              $is_found = 1 ;
  24.              last TARGET_LOOP;
  25.          } 
  26.     }   
  27.   close $My_File_FP;   
  28.   return $is_found; 
  29. }
  30.  
__________________________________________________ ______


Hope my approach is correct..

thanks for replying ASAP..
Regards,
Jun 21 '10 #1
Share this Question
Share on Google+
1 Reply


Expert Mod 100+
P: 589
If you want to insert in the "middle", then you could load the file into an array and use splice to insert the line where needed.
http://perldoc.perl.org/functions/splice.html

For traversing the folders, you'd use File::Find
http://search.cpan.org/~jesse/perl-5...b/File/Find.pm
Jun 21 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.