skip to main | skip to sidebar

Perl Programming Language Tutorial

Pages

  • Home
 
  • RSS
  • Twitter
Wednesday, October 17, 2012

Cross Referencing script

Posted by Raju Gupta at 3:30 AM – 0 comments
 

Script to cross reference sybase tables and stored procedures of a given database with perl scripts (.cgi, .pl, .pm extensions)


  #!/ms/dist/perl5/bin/perl5.6

use strict;
use Sybase::DBlib;
use Benchmark;

my $t1 = new Benchmark;

if( scalar (@ARGV) < 2 ) {
 die "Need the directory of perl scripts and/or DB name!\n";
}

my $dir = $ARGV[ 0 ];
my $dbname = $ARGV[ 1 ];
my $user = $ARGV[ 2 ];
my $pass = $ARGV[ 3 ];
my $server = $ARGV[ 4 ];

my $dbh = new Sybase::DBlib( $user, $pass, $server ) or die "\nCannot login to $server\n";
my $db = $dbh->dbuse( $dbname );

opendir DIR, $dir or die "Cannot open directory $dir for read\n";

my @files = readdir( DIR );

closedir DIR;

my $sql = "select tab.name  from sysobjects tab where tab.type='U'";

my @tab;
my @col;

#Replace the nsql function to call using HASH

my $rc = $dbh->nsql( $sql, "ARRAY", sub{
          push @tab, $_[ 0 ];
          push @col, $_[ 1 ];
         });

if( $DB_ERROR ) {
 die "Error encountered during select of table names- $DB_ERROR\n";
}

my %TblHash;
my $temp;

foreach my $tab1 ( @tab ) {
 my $sql;

 $sql = "sp_depends $tab1";

 my @depends;

 $dbh->nsql( $sql, "ARRAY", sub{
         if(( defined($_[ 1 ] )) and ($_[ 1 ] eq "stored procedure" )){
          my $proc1 = substr($_[ 0 ],4);
          $proc1 =~ s/\s+//g;
          push @depends, $proc1;
         }
         });

 print "Searching for table - $tab1\n";

 foreach my $fl1 ( @files ) {
  if( $fl1 =~ 'cgi$|perl$|pm$|perl$' ) {

   open (FIL, "$dir/$fl1") or warn "cannot open - $fl1\n";
    
   my $line;

   while( $line =  ) {

    if( $line =~ $tab1 and $line !~ '^#') {
     if( $TblHash{$tab1} !~ $fl1 ) {
       $TblHash{$tab1} = $TblHash{$tab1}."$fl1,";
     }
    }
    else {
     foreach (@depends) {
      if( $line =~ $_ ) {
       if( $TblHash{$tab1} !~ $fl1 ) {
         $TblHash{$tab1} = $TblHash{$tab1}."$fl1,";
       }
      }
     }
    }
   }
   close FIL;
  }
 }
}     

open OUT,">report.txt" or die "cannot open report for write!\n";

foreach (keys (%TblHash)) {
 my @scriptfiles = split /,/,substr($TblHash{$_},0,-1);

 print OUT "\n--------------------------\n";
 print OUT $_,"\n--------------------------\n";

 foreach (@scriptfiles){
  print OUT $_,"\n";
 }

 print OUT "\n++++++++++++++++++++++++++\n\n";
}

close OUT;


$dbh->dbclose();

my $t2 = new Benchmark;

my $timediff = timediff( $t2, $t1 );

print "Total time for exec - ",timestr($timediff),"\n";

Email This BlogThis! Share to X Share to Facebook

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments (Atom)
  • Popular
  • Recent
  • Archives

Popular Posts

  • Perl function to compare two dates
    This function can be used to compare two dates using PERL. The function accepts two string(date) arguments, let's say date1 and date2...
  • Perl script to find files older than x minutes
    This script can be used to find files in a windows directory older than 40 min. List can be emailed to a user also. Script can be modi...
  • Perl function to check whether the passed path is empty or not
    The function makes sure that the path (directory and/or file) passed to it as an Input parameter is empty or not. use constant SUCCESS ...
  • File size and modified time
    When we pass the file name (include full path) as parameter,It will give us the size of the file and last modified time. sub getFileSt...
  • Perl function to trim leading and trailing spaces from a string
    Leading and trailing spaces, if any present, are trimmed and the string is returned back to the caller. If a NULL string is passed, the func...
  • Fix Message Reader from Log
    Various subroutines of the package FixUtil can be used to read fix message (tag, value pair). Fix message can be extracted. Tag and Value ca...
  • Login screen using Perl
    This code snippet takes one parameter for default user and displays a login screen asking for user name and password . It aslo provides a...
  • Sepearte First name and last name by using PERL regular expressions
    We are using PERL regular expression. We are using 3 functions prxparse,prxmatch and prxposn. Prxparse takes regularexpression an...
  • Perl function to check whether file or dir name passed to it readable or not
    The function makes sure that the path (directory and/or file) passed to it as an Input parameter is readable or not   use constant SU...
  • Password encryption-Perl Script
    It is used to encrypt the password. Password to be encrypted is passed as agrument to this subroutine when called. sub encrypt_pass { ...
Powered by Blogger.

Archives

  • ▼  2012 (24)
    • ▼  October (24)
      • Sepearte First name and last name by using PERL re...
      • Validate Info-Perl Script
      • Print Message--Perl Script
      • Password encryption-Perl Script
      • Convert to Seconds--Perl script
      • File size and modified time
      • Convert to minute-Perl script
      • Login screen using Perl
      • Huge text file comparator
      • Parse Input - Perl Script
      • Get colored difference - Perl Script
      • Perl function to compare two dates
      • Script to rotate any log file
      • Cross Referencing script
      • Random Bunch Creation in Perl
      • NASDAQ Status checker using Perl
      • Fix Message Reader from Log
      • Perl script to find files older than x minutes
      • Perl function to check whether the passed path is ...
      • Date Arimatic
      • Perl function to trim leading and trailing spaces ...
      • Perl function to check whether the passed path is ...
      • Perl function to check whether file or dir name pa...
      • Perl function to check whether the passed path is ...
 

Followers

Labels

  • File Searching Example (1)
  • Perl Date Example (2)
  • Perl Encryption Example (1)
  • Perl File Example (2)
  • Validation Example (1)
 
 
© 2011 Perl Programming Language Tutorial | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger

  • Tweet
  • t