Author: hmbrand
Date: Tue Oct 27 07:42:13 2009
New Revision: 13434

Modified:
   dbi/trunk/lib/DBD/File.pm

Log:
Code cleanup, consistency in returns.

Modified: dbi/trunk/lib/DBD/File.pm
==============================================================================
--- dbi/trunk/lib/DBD/File.pm   (original)
+++ dbi/trunk/lib/DBD/File.pm   Tue Oct 27 07:42:13 2009
@@ -31,9 +31,10 @@
 
 use strict;
 
+use Carp;
 use vars qw( @ISA $VERSION $drh $valid_attrs );
 
-$VERSION = "0.37";
+$VERSION = "0.38";
 
 $drh = undef;          # holds driver handle(s) once initialised
 
@@ -54,7 +55,8 @@
     $attr ||= {};
     {  no strict "refs";
        unless ($attr->{Attribution}) {
-           $class eq "DBD::File" and $attr->{Attribution} = "$class by Jeff 
Zucker";
+           $class eq "DBD::File" and
+               $attr->{Attribution} = "$class by Jeff Zucker";
            $attr->{Attribution} ||= ${$class . "::ATTRIBUTION"} ||
                "oops the author of $class forgot to define this";
            }
@@ -78,7 +80,7 @@
 
     $file eq "." || $file eq ".."      and return;
 
-    my ($ext, $req) = ("", 0, 0);
+    my ($ext, $req) = ("", 0);
     if ($data->{f_ext}) {
        ($ext, my $opt) = split m/\//, $data->{f_ext};
        if ($ext && $opt) {
@@ -92,10 +94,10 @@
     # Fully Qualified File Name
     my $fqfn;
     unless ($quoted) { # table names are case insensitive in SQL
-       local *DIR;
-       opendir DIR, $dir;
-       my @f = grep { lc $_ eq lc $file } readdir DIR;
+       opendir my $dh, $dir or croak "Can't open '$dir': $!";
+       my @f = grep { lc $_ eq lc $file } readdir $dh;
        @f == 1 and $file = $f[0];
+       closedir $dh or croak "Can't close '$dir': $!";
        }
     $fqfn = File::Spec->catfile ($dir, $file);
 
@@ -167,7 +169,7 @@
            sql_statement_version => 1, # S:S version
            };
        }
-    $this->STORE ("Active", 1);
+    $this->STORE (Active => 1);
     return set_versions ($this);
     } # connect
 
@@ -216,7 +218,7 @@
        $file ne File::Spec->updir () && -d $d and
            push @dsns, "DBI:$driver:f_dir=$d";
        }
-    @dsns;
+    return @dsns;
     } # data_sources
 
 sub disconnect_all
@@ -239,7 +241,7 @@
 
 sub ping
 {
-    return (shift->FETCH ("Active")) ? 1 : 0;
+    ($_[0]->FETCH ("Active")) ? 1 : 0;
     } # ping
 
 sub prepare ($$;@)
@@ -282,7 +284,7 @@
            $sth->STORE ("NUM_OF_PARAMS", scalar ($stmt->params ()));
            }
        }
-    $sth;
+    return $sth;
     } # prepare
 
 sub csv_cache_sql_parser_object
@@ -302,8 +304,8 @@
 
 sub disconnect ($)
 {
-    shift->STORE ("Active", 0);
-    1;
+    $_[0]->STORE (Active => 0);
+    return 1;
     } # disconnect
 
 sub FETCH ($$)
@@ -345,17 +347,17 @@
        # not implemented yet
        # my $class = $dbh->FETCH ("ImplementorClass");
        #
-       # !$dbh->{f_valid_attrs}->{$attrib} && 
!$dbh->{sql_valid_attrs}->{$attrib} and
+       # !$dbh->{f_valid_attrs}{$attrib} && !$dbh->{sql_valid_attrs}{$attrib} 
and
        #    return $dbh->set_err ($DBI::stderr, "Invalid attribute '$attrib'");
        #  $dbh->{$attrib} = $value;
 
        if ($attrib eq "f_dir") {
            -d $value or
-               return $dbh->set_err ($DBI::stderr, "No such directory 
'$value'")
+               return $dbh->set_err ($DBI::stderr, "No such directory 
'$value'");
            }
        if ($attrib eq "f_ext") {
-           $value eq "" || $value =~ m{^\.\w+(?:/[rR]*)?$}
-               or carp "'$value' doesn't look like a valid file extension 
attribute\n";
+           $value eq "" || $value =~ m{^\.\w+(?:/[rR]*)?$} or
+               carp "'$value' doesn't look like a valid file extension 
attribute\n";
            }
        $dbh->{$attrib} = $value;
        return 1;
@@ -367,6 +369,7 @@
 {
     my $dbh = shift;
     $dbh->SUPER::FETCH ("Active") and $dbh->disconnect ;
+    undef $dbh->{csv_sql_parser_object};
     } # DESTROY
 
 sub type_info_all ($)
@@ -455,7 +458,7 @@
                                    NAMES => $names,
                                    });
        $sth or $dbh->set_err ($DBI::stderr, $dbh2->errstr);
-       $sth;
+       return $sth;
        } # table_info
     }
 
@@ -467,7 +470,7 @@
     while (my $ref = $sth->fetchrow_arrayref ()) {
        push @tables, $ref->[2];
        }
-    @tables;
+    return @tables;
     } # list_tables
 
 sub quote ($$;$)
@@ -490,7 +493,7 @@
     $str =~ s/\'/\\\'/sg;
     $str =~ s/\n/\\n/sg;
     $str =~ s/\r/\\r/sg;
-    "'$str'";
+    return "'$str'";
     } # quote
 
 sub commit ($)
@@ -498,7 +501,7 @@
     my $dbh = shift;
     $dbh->FETCH ("Warn") and
        carp "Commit ineffective while AutoCommit is on", -1;
-    1;
+    return 1;
     } # commit
 
 sub rollback ($)
@@ -506,7 +509,7 @@
     my $dbh = shift;
     $dbh->FETCH ("Warn") and
        carp "Rollback ineffective while AutoCommit is on", -1;
-    0;
+    return 0;
     } # rollback
 
 # ====== STATEMENT 
=============================================================
@@ -522,19 +525,19 @@
     my ($sth, $pNum, $val, $attr) = @_;
     if ($attr && defined $val) {
        my $type = ref $attr eq "HASH" ? $attr->{TYPE} : $attr;
-       if (   $attr == DBI::SQL_BIGINT ()
-           || $attr == DBI::SQL_INTEGER ()
+       if (   $attr == DBI::SQL_BIGINT   ()
+           || $attr == DBI::SQL_INTEGER  ()
            || $attr == DBI::SQL_SMALLINT ()
-           || $attr == DBI::SQL_TINYINT ()
-           ) {
+           || $attr == DBI::SQL_TINYINT  ()
+              ) {
            $val += 0;
            }
        elsif ($attr == DBI::SQL_DECIMAL ()
-           || $attr == DBI::SQL_DOUBLE ()
-           || $attr == DBI::SQL_FLOAT ()
+           || $attr == DBI::SQL_DOUBLE  ()
+           || $attr == DBI::SQL_FLOAT   ()
            || $attr == DBI::SQL_NUMERIC ()
-           || $attr == DBI::SQL_REAL ()
-           ) {
+           || $attr == DBI::SQL_REAL    ()
+              ) {
            $val += 0.;
            }
        else {
@@ -542,7 +545,7 @@
            }
        }
     $sth->{f_params}[$pNum - 1] = $val;
-    1;
+    return 1;
     } # bind_param
 
 sub execute
@@ -574,7 +577,7 @@
     if ($stmt->{NUM_OF_FIELDS}) {    # is a SELECT statement
        $sth->STORE (Active => 1);
        $sth->FETCH ("NUM_OF_FIELDS") or
-           $sth->STORE ("NUM_OF_FIELDS", $stmt->{NUM_OF_FIELDS})
+           $sth->STORE ("NUM_OF_FIELDS", $stmt->{NUM_OF_FIELDS});
        }
     return $result;
     } # execute
@@ -583,29 +586,29 @@
 {
     my $sth = shift;
     $sth->SUPER::STORE (Active => 0);
-    delete $sth->{f_stmt}->{data};
+    delete $sth->{f_stmt}{data};
     return 1;
     } # finish
 
 sub fetch ($)
 {
     my $sth  = shift;
-    my $data = $sth->{f_stmt}->{data};
+    my $data = $sth->{f_stmt}{data};
     if (!$data || ref $data ne "ARRAY") {
        $sth->set_err ($DBI::stderr,
            "Attempt to fetch row without a preceeding execute () call or from 
a non-SELECT statement"
            );
-       return
+       return;
        }
     my $dav = shift @$data;
     unless ($dav) {
        $sth->finish;
-       return
+       return;
        }
     if ($sth->FETCH ("ChopBlanks")) {
        $_ && $_ =~ s/\s+$// for @$dav;
        }
-    $sth->_set_fbav ($dav);
+    return $sth->_set_fbav ($dav);
     } # fetch
 *fetchrow_arrayref = \&fetch;
 
@@ -648,11 +651,13 @@
 {
     my $sth = shift;
     $sth->SUPER::FETCH ("Active") and $sth->finish;
+    undef $sth->{f_stmt};
+    undef $sth->{f_params};
     } # DESTROY
 
 sub rows ($)
 {
-    shift->{f_stmt}->{NUM_OF_ROWS};
+    return $_[0]->{f_stmt}{NUM_OF_ROWS};
     } # rows
 
 package DBD::File::Statement;
@@ -740,7 +745,7 @@
     my $class = ref $self;
     $class =~ s/::Statement/::Table/;
     bless $tbl, $class;
-    $tbl;
+    return $tbl;
     } # open_table
 
 package DBD::File::Table;
@@ -779,7 +784,7 @@
     my ($self, $data) = @_;
     $self->{fh}->truncate ($self->{fh}->tell ()) or
        croak "Error while truncating " . $self->{file} . ": $!";
-    1;
+    return 1;
     } # truncate
 
 1;

Reply via email to