dbmopen question

D

Default

Just wondering if anyone can help me figure out what this dbmopen option
is all about.

dbmopen(my %WORDS, $dbfile,0666) # There see that number?

What is that? No better yet where in perldoc can i find information on this?
Please help I've got a feeling that this is something I need to know about.

If anyone would be kind enough to comment on the program that would be
greatly appreciated. This is only my 25th ish program so im sure it's far from
efficient.

Here is the code.

#!
use strict;
use warnings;
print "\n" . ' ' . '='x78 . "\n Creates a database of words and how " .
"often each word has been seen.\n " . '='x78 . "\n\n";
our $files = lc(shift) || help() && die "\n";
my $opt1 = lc(shift);
my $opt2 = lc(shift);
my $opt3 = lc(shift);
my $opt4 = lc(shift);
my $opt5 = lc(shift);
my $opt6 = lc(shift);
my $dbfile = "words";
my $file = "";
if ($files eq "-?") {help();die "\n";}
if ($opt1 eq "-?" or $opt2 eq "-?" or $opt3 eq "-?" or $opt4 eq "-?" or
$opt5 eq "-?" or $opt6 eq "-?") {help();}
if ($files eq "-n" or $opt1 eq "-n" or $opt2 eq "-n" or $opt3 eq "-n" or
$opt4 eq "-n" or $opt5 eq "-n" or $opt6 eq "-n")
{
my $new_db = "";
print ' ' . '='x78 . "\n";
print " Select or create a database. " .
'The default database is: "' . "$dbfile" . '"' . "\n";
print ' ' . '='x78 . "\n\n";
while ($new_db eq "")
{
print 'Enter the name of the database: ';
chomp ($new_db = lc<STDIN>);
}
my $new_db_1 = "$new_db" . ".pag";
my $new_db_2 = "$new_db" . ".dir";
if (!-e $new_db_1 and !-e $new_db_2)
{
print "\n".'Create new database "'."$new_db".'" (y or n) ';
chomp (my $verify = lc<STDIN>);
die "\n".'Quitting: "'."$new_db".'" was not created'."\n"
unless ($verify eq "y");
}
else {print 'Selecting the "'."$new_db".'" database.'."\n\n";}
$dbfile = $new_db;
}
if ($files eq "-w" or $opt1 eq "-w" or $opt2 eq "-w" or $opt3 eq "-w" or
$opt4 eq "-w" or $opt5 eq "-w" or $opt6 eq "-w")
{
print "Updating database...\n";
dbmopen(my %WORDS,"$dbfile",0644)||die "\nCan't open $dbfile #!\n";
foreach $file (<${files}>)
{
open(READFILE, "$file") || warn "\nCan't open $file $!\n";
while (<READFILE>)
{
foreach my $word (split (/\W+/))
{
if ($opt1 eq "-i" or $opt2 eq "-i" or $opt3 eq "-i"
or $opt4 eq "-i" or $opt5 eq "-i"
or $opt6 eq "-i")
{$word = lc($word);}
$WORDS{$word}++;
}
}
close (READFILE) || warn "\nCan't close $file $!\n";
}
dbmclose(%WORDS) || warn "\nCan't close $dbfile #!\n";
print "\nFinished updating database.\n\n";
}
if ($files eq "-r" or $opt1 eq "-r" or $opt2 eq "-r" or $opt3 eq "-r" or
$opt4 eq "-r" or $opt5 eq "-r" or $opt6 eq "-r")
{
print ' ' . '='x78 . "\n This will read the" . '"' . "$dbfile" .
'" database.' . "\n" . ' ' . '='x78 . "\n\n";
print "Press <Enter> to continue.";
my $pause = <STDIN>; $pause = undef; print "\n\n";
dbmopen(my %WORDS,"$dbfile",0644)||die "Can't open $dbfile:\n#!\n";
while ((my $key, my $value) = each(%WORDS))
{print "$key has been seen $value times.\n";}
dbmclose(%WORDS) || warn "\nCan't close $dbfile #!\n";
}
if ($files eq "-c" or $opt1 eq "-c" or $opt2 eq "-c" or $opt3 eq "-c" or
$opt4 eq "-c" or $opt5 eq "-c" or $opt6 eq "-c")
{
print "\n" . ' ' . '='x78 . "\n";
print ' This will clear the "' . "$dbfile" . '" database.';
print "\n" . ' ' . '='x78 . "\n\n";
print 'Are you sure you wish to do this (y or n)? ';
chomp (my $verify = lc<STDIN>);
print "\n";
die 'Quitting: The "'."$dbfile".'" database was not cleared.'."\n"
unless ($verify eq "y");
dbmopen(my %WORDS, $dbfile,0666)||die "Can't open $dbfile: $!\n";
%WORDS = ();
dbmclose (%WORDS);
die '"' . "$dbfile" . '" has been cleared.' . "\n";
}
print "\nDone.\n";

#Subroutines#
sub help
{
print <<ENDTEXT;
USAGE: perl 17_1.plx [file or wildcard] [options]

OPTIONS: -? Help.
-c Clear the database.
-i Ignore case while searching words.
-n Create a new database, or select a database to read.
-r Read from the database.
-w Write to the database.
NOTES: Default database will be used unless -n is specified.
You will not be able to write to the database unless
files are specified and the -w switch is issued.
EXAMPLES:
perl 17_1.plx file.txt -w Updates the default database with
information obtained from the
specified files.
perl 17_1.plx *.* -n -w -r Creates a new database from the
information contained in the
specified files, and then reads it.
perl 17_1.plx -n -r Selects an alternate database and
reads the contents.
perl 17_1.plx -r -n -c Selects an alternate database and
clears then reads the contents.
perl 17_1.plx -r -? Reads from the default database and
displays this help screen.

ENDTEXT
}
 
B

Ben Morrow

Just wondering if anyone can help me figure out what this dbmopen
option is all about.

dbmopen(my %WORDS, $dbfile,0666) # There see that number?

What is that? No better yet where in perldoc can i find information
on this?

perldoc -f dbmopen

and for an explanation of what the number means, perldoc -f umask.

#!
use strict;
use warnings;
print "\n" . ' ' . '='x78 . "\n Creates a database of words and how " .
"often each word has been seen.\n " . '='x78 . "\n\n";
our $files = lc(shift) || help() && die "\n";

Why our()? my would be better.
If you're not going to die with a message, use exit.
my $opt1 = lc(shift);
my $opt2 = lc(shift);
my $opt3 = lc(shift);
my $opt4 = lc(shift);
my $opt5 = lc(shift);
my $opt6 = lc(shift);

my %opts;
$_ = 1 for @opts{map {lc} @ARGV};

then you can test for individual options with 'if ($opts{"-?"})'
&c. Or alternatively, use one of the Getopt modules from CPAN.

die "\n".'Quitting: "'."$new_db".'" was not created'."\n"

Don't put "\n" on the end of warn and die messages: it causes Perl to
omit valuable information about where the error occurred.

Ben
 
U

Uri Guttman

BM> my %opts;
BM> $_ = 1 for @opts{map {lc} @ARGV};

if you are going to do some map/slice stuff at least do it in a clear
style:

my %opts = map { lc => 1 } @ARGV ;

but as you said using some getopts module is best.

uri
 
B

Ben Morrow

Uri Guttman said:
BM> my %opts;
BM> $_ = 1 for @opts{map {lc} @ARGV};

if you are going to do some map/slice stuff at least do it in a clear
style:

my %opts = map { lc => 1 } @ARGV ;

Ach, yes, I always forget that a map can return more or fewer elements
than its input.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top