home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / FindSqlFile.pm < prev    next >
Encoding:
Perl POD Document  |  2002-12-13  |  1.8 KB  |  86 lines

  1. #!/usr/local/bin/perl
  2.  
  3. package DBI::Shell::FindSqlFile;
  4.  
  5. use strict;
  6. use File::Find ();
  7. use File::Basename;
  8. use File::Spec;
  9.  
  10. use vars qw($VERSION);
  11.  
  12. $VERSION = sprintf( "%d.%02d", q$Revision: 11.91 $ =~ /(\d+)\.(\d+)/ );
  13.  
  14. # Set the variable $File::Find::dont_use_nlink if you're using AFS,
  15. # since AFS cheats.
  16.  
  17. # for the convenience of &wanted calls, including -eval statements:
  18. use vars qw/*name *dir *prune @found $to_find_file $debug/;
  19. *name   = *File::Find::name;
  20. *dir    = *File::Find::dir;
  21. *prune  = *File::Find::prune;
  22.  
  23. @found = ();
  24. $to_find_file = undef;
  25. $debug = 0;
  26.  
  27. sub look_for_file {
  28.     my $self = shift;
  29.     my $file = shift;
  30.     my ($base, $dir, $ext) = fileparse($file,'\..*?');
  31.  
  32.     $debug = $self->{debug};
  33.  
  34.     # print "file $file : concat $dir$base$ext\n";
  35.  
  36.     # Work-around to fileparse adding current directory.
  37.     $dir = undef unless ( $file eq "$dir$base$ext" );
  38.  
  39.     unless ($ext) {
  40.         $ext = q{.sql};
  41.     }
  42.     # If a directory is defined, return to caller
  43.     if ($dir) {
  44.         return ( "$dir$base$ext" );
  45.     };
  46.  
  47.     $to_find_file = qq{$base$ext};
  48.  
  49.     $self->log("calling find with $to_find_file") if $self->{debug};
  50.  
  51.  
  52. # Split the sqlpath, then determine if any of the directories are valid.
  53.     my @search_path = map { -d $_ ? $_ : () } split(/:/,
  54.            defined $self->{sqlpath} ? $self->{sqlpath} : ()
  55.            );
  56.         # ,  (exists $ENV{DBISH_SQL_PATH} ?  $ENV{DBISH_SQL_PATH} : ()) );
  57.  
  58.     $self->log( "search path: " . join( "\n", @search_path ) )
  59.         if $self->{debug};
  60.  
  61.     # Traverse desired filesystems
  62.     File::Find::find(
  63.         {
  64.               wanted     => \&wanted
  65.             , no_chrdir     => 1
  66.             , bydepth    => 0
  67.         }, 
  68.         @search_path);
  69.  
  70.  
  71.     return shift @found if @found;
  72.  
  73. return;
  74. }
  75.  
  76. sub wanted {
  77.     (/^.*$to_find_file\z/is && print "Found $to_find_file file
  78.     $name\n" ) if $debug;
  79.     /^.*$to_find_file\z/is && push @found, $name;
  80.     $prune = 1 if ( -d $dir and -d $name and $dir ne $name );
  81. }
  82.  
  83. 1;
  84.  
  85. __END__
  86.