File Coverage

blib/lib/DBI/Shell/FindSqlFile.pm
Criterion Covered Total %
statement 32 35 91.4
branch 14 22 63.6
condition 5 9 55.5
subroutine 7 7 100.0
pod 0 2 0.0
total 58 75 77.3


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