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

pattern matching

P: 9
I have tried the following code:

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use Switch;
  3. $i=0;
  4. open(FILE,"$ARGV[0]") || die "cannot open file";
  5. @lines = <FILE>;
  6. close(FILE);
  7. open(wfile,">$ARGV[1]");
  8. while ($lines[$i]) {
  9.     $line = $lines[$i];
  10.     if ( ($line =~ m/(^\w+)(\s+)(\()(\))/) 
  11.         || ($line =~ m/(^\w+)(\s+)(\()(\s+)(\))/)
  12.         || ($line =~ m/(^\s+)(\w+)(\s+)(\()(\s+)(\))/)
  13.         || ($line =~ m/(^\s+)(\w+)(\s+)(\()(\))/)
  14.         || ($line =~ m/(^\s+)(\w+)(\()(\s+)(\))/)
  15.         || ($line =~ m/(^\s+)(\w+)(\()(\))/)
  16.         || ($line =~ m/(^\w+)(\()(\))/)
  17.         || ($line =~ m/(^\w+)(\()(\s+)(\))/)
  18.     ) {
  19.         $b = $1; $a = $2;
  20.         if ($1 =~ m/(\s+)/) {
  21.             $b = $a;
  22.         }
  23.         $x = 1;
  24.         print wfile $line;
  25.         if ($line =~ m/\{/) {
  26.             $z = 1;
  27.             print wfile "echo \"calling func. $b\"\n";
  28.         }
  29.         $i++;
  30.         $line = $lines[$i];
  31.         if ($line =~ m/(^\{)/) {
  32.             $z = 1;
  33.             print wfile $line;
  34.             print wfile "echo  \"calling func. $b\"\n";
  35.         }
  36.         while ($z ==1 ) {
  37.             $d = $line;
  38.             $i++;
  39.             $line = $lines[$i];
  40.             switch($line) {
  41.                 case m/\{/ {
  42.                     $x++;
  43.                     print wfile $line
  44.                 }
  45.                 case m/\}/ {
  46.                     $x--;
  47.                     if ($x!=0) {
  48.                         print wfile $line
  49.                     }
  50.                 }
  51.                 else {
  52.                     print wfile $line
  53.                 }
  54.             }
  55.             if ($x == 0) {
  56.                 $z=0;
  57.                 print wfile "echo \"return from $b\"\n";
  58.                 print wfile $line
  59.             }
  60.         }
  61.  
  62.     } else {
  63.         print wfile $line;
  64.     }
  65.     $i++;
  66. }
  67. close(wfile);
  68.  
this code is for identifying a function in shell script & inserting echo statements when it is been called and returned.....
can u suggest someting to make it more efficient..
Sep 6 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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