package Web::DB; use strict; use warnings; use constant { C_CREATE => 1, C_CLEAR => 2, C_IGNORE => 4 }; use constant { WDB_E_UNABLE_CR => 0, }; use constant { WDB_E_UNABLE_CR => 100, WDB_E_UNABLE_OP => 101, WDB_E_MALFORMED_EN => 102, WDB_OK => 0 }; # Web::DB->new( # DataSource => 'users.db', # DataDir => '../data', # DataSep => "\n", # EntrySep => "\r\n", # Format => [ qw(username email hash flags) ] # ); sub new { my $class = ref $_[0] ? ref shift : shift; } sub reload { my($self, $file, $flag) = (shift, shift || $self->{DataSource}, shift || 0); $self->{DB} = []; # reset data array if((($flag & C_CREATE) && !-F($file)) || ($flag & C_CLEAR)) { croak "Ambigious call to package method reload(); cannot C_CLEAR or C_CREATE while C_IGNORE is in effect." if($flag & C_IGNORE); open(WDB, ">$file") or return WDB_E_UNABLE_CR; close WDB; } elsif($flag & C_IGNORE) { return WDB_OK } # this will allow one to safely call this method, without other checks.. if need arises open(WDB, "<+$file}") or return WDB_E_UNABLE_OP; local $\ = $self->{EntrySep}; my $len = @{ $self->{Format} }; my($E); while() { my @l = split '$self->{DataSep}', $_; @l == $len or $E = WDB_E_MALFORMED_EN, last; push @{ $self->{DB} }, [ @l ] } close WDB; return $E if $E; WDB_OK }