homepage Welcome to WebmasterWorld Guest from 54.237.235.12
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Can't open database file
typomaniac



 
Msg#: 4326841 posted 12:10 pm on Jun 16, 2011 (gmt 0)

I'm trying to use a text type DBM file but can't extract the values from the keys. I'm getting a message in the browser that says "Can't open english.db:Permission denied". I did alot of searching on this one to no avail.

Script looks like this:

#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;
print"Content-Type:text/html\n\n";

my(%hexcodes,$dbm,%HASH,%hash,$red);
my $dbm="colors.db";
tie (%HASH, "DB_File", $dbm)or die "Can't open $dbm:$!\n";

my $red=$hexcodes{'red'};
print"$red";

untie %hash;

colors.db looks like this(separate file)I tried using the .txt extension but getting the same results.

%hexcodes('red' => 'ff0000',
'black' => '000000',
'white' => 'ffffff'
)

 

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4326841 posted 5:04 pm on Jun 16, 2011 (gmt 0)

Is that your full script? Where does english.db come in?

Is the .db in cgi-bin? Try storing it in a different directory and using the full path.

/cgi-bin/yourscript.cgi
/httpdocs/data/colors.db

my $dbm = $ENV{'DOCUMENT_ROOT'} . "/data/colors.db";

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4326841 posted 6:16 pm on Jun 16, 2011 (gmt 0)

Is the database-file read- and writable by the user the cgi-script runs as?
If apache is not configured with suexec/mod_suid2/mod_ruid, that's usually not the same user you upload stuff as.
Try chmod'ing the db-file to 777 (read & write for all) and see if that works.

typomaniac



 
Msg#: 4326841 posted 12:51 am on Jun 18, 2011 (gmt 0)

I was living up to my name typo when I made this post. The file english.db was the mistake. Any way got that fixed. Problem now is when I open the script as posted below I get a "0"(zero) appearing on the browser screen.

#!/usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;
print"Content-Type:text/html\n\n";
my(%colors,$dbfile,%HASH,%hash);
my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt ";
tie (%HASH, "DB_File", $dbfile)or die "Can't open $dbfile: $!\n";

my %colors;
print"$colors{'ffffff'}";
print"$colors{'000000'}";
print"$colors{'ff0000'}";
untie %hash

colors.txt is as follows

%colors=(
'ff0000' =>'red'
'000000' => 'black'
'ffffff' => 'white'
)

If I add any html anywhere on the page it appears and the zero goes away but am getting nothing from the text file. I tried putting the .txt file in the cgi-bin and also the httdocs area with the same results.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4326841 posted 2:44 am on Jun 18, 2011 (gmt 0)

have you tried running your cgi script from the command line to see the actual output?

also, unless i'm missing something the %colors hash appears to be unevaluated in your script.

typomaniac



 
Msg#: 4326841 posted 7:56 am on Jun 18, 2011 (gmt 0)

phranque....not sure how one goes about running from the command line. Also, how would I go about evaluating the %colors hash? I'm wanting to learn. I'm just getting over the shock of making everything in the strict mode.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4326841 posted 9:09 am on Jun 18, 2011 (gmt 0)

if your script file were named colordb.cgi and you are in the directory containing your script you could enter the following command at the shell prompt:
/usr/bin/perl colordb.cgi
and your print statements would appear on standard output.

regarding hash evaluation, TMTOWTDI:


my %colors = (
ff0000 => 'red'
000000 => 'black'
ffffff => 'white'
);

or

my %colors = ();
$colors{ff0000} = 'red';
$colors{000000} = 'black';
$colors{ffffff} = 'white';

or

my %colors = ('ff0000','red','000000','black','ffffff','white');

or as an anonymous hash reference

my $colors_ref = {
ff0000 => 'red'
000000 => 'black'
ffffff => 'white'
};


typomaniac



 
Msg#: 4326841 posted 11:25 am on Jun 18, 2011 (gmt 0)

I tried all you mentioned to no avail. I typed in the filename following the shebang and nothing changed. Not sure if I have access to the shell prompt though as I'm on a hosted server---searched their knowledge base and found nothing on the subject.

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4326841 posted 3:19 pm on Jun 20, 2011 (gmt 0)

colors.txt is as follows


On further investigation, it looks to me like you're storing it in the DB file wrong. DB_FILE reads database formats - you're storing an array.

Try a text file with tab delimited values?

red ff0000
black 000000
white ffffff

Or vice versa . . . presuming you want keys on the left, values on the right . . .

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4326841 posted 12:35 am on Jun 21, 2011 (gmt 0)

I typed in the filename following the shebang and nothing changed.

the shebang is the first line of your script.
the shell prompt is what you see when you open a command shell.

if you're on a shared host you might not have shell access.

i don't see anything in your script binding the %color hash to anything or assigning any values to it.
therefore it appears you declared the hash and then tried to access a key/value that doesn't exist.

typomaniac



 
Msg#: 4326841 posted 1:55 am on Jun 21, 2011 (gmt 0)

rocknbil: I like the idea of a text file with tab delimited values but how do I access the values?

phranque: I've changed the "tie" statement to:
tie (%colors,"DB_File",$dbfile, O_CREAT|O_RDWR, 0777)or die "Can't open $dbfile: $!\n";
When accessed at [teresafbc.org...] which includes the following line:
print"The opposite of $colors{'000000'} is $colors{'ffffff'}";
The text shows but $colors{'000000'} and $colors{'ffffff'} does not.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4326841 posted 11:08 am on Jun 21, 2011 (gmt 0)

you should use DB_File to create and write the file for you:


#! /usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;

print "Content-Type:text/html\n\n";

my %colors;
my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt ";

# delete any existing colors.txt file
# or remove this unlink if you are adding to an existing db file
unlink $dbfile;

# tie %colors hash to and create db file in color.txt
# keep defaults in this case
tie (%colors"DB_File"$dbfileor die "Can't open $dbfile$!\n";

# (re)evaluate db in colors.txt by evaluating key/value pairs in %colors hash as follows
%colors=(
'ff0000' => 'red'
'000000' => 'black'
'ffffff' => 'white'
);

print "$colors{'ffffff'}";
print "$colors{'000000'}";
print "$colors{'ff0000'}";


then you can reverse-engineer the colors.txt file!
=8)

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4326841 posted 4:08 pm on Jun 21, 2011 (gmt 0)

^ ^ ^ Yeah that,


rocknbil: I like the idea of a text file with tab delimited values but how do I access the values?


The module should to it for you, just create a file like I showed by example with tabs between key/value and give it a try, see what happens.

typomaniac



 
Msg#: 4326841 posted 3:39 am on Jun 22, 2011 (gmt 0)

Must be something on the server end. I uploaded the script as you wrote it out and as long as the hash was on the script page I could get the values but trying to get them from the txt file only prints out a 0 on the screen unless I have html in which case the 0 goes away and the html prints. The server complained about using tabs and no commas.

I contacted the server host on the matter and their last reply was "Please note that since we are a hosting provider unfortunately we do not provide support for designing website and customerís coding related issues. I have uploaded a test script at [@#$%^...] and it is showing fine. Please verify the scripts once again from your end."

The only thing they did hwas make a basic "Hi World" type of script and told me all is well on their end. Actually, when I first uploaded the script it was the same as what phranque presented (I got it out of the perl cookbook with a few minor adjustments for the situation. The deal about english.db not being was a dumb mistake on my part which happened after the initial upload).

Maybe its time to save up and get another host. I would like to thank you so much for all your help.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4326841 posted 9:41 am on Jun 22, 2011 (gmt 0)

your host is right.
as long as they can demonstrate that a "hello world" script works the application-specific work is yours.


ok try this.
create one script named createdb.pl:


#! /usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;

print "Content-Type:text/html\n\n";

my %colors;
my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt ";

# delete any existing colors.txt file
unlink $dbfile;

# tie %colors hash to and create db file in color.txt
tie (%colors"DB_File"$dbfileor die "Can't open $dbfile$!\n";

# (re)evaluate db in colors.txt by evaluating key/value pairs in %colors hash as follows
%colors=(
'ff0000' => 'red'
'000000' => 'black'
'ffffff' => 'white'
);

print "$colors{'ffffff'}";
print "$colors{'000000'}";
print "$colors{'ff0000'}";

untie %colors;



then create another script named readdb.pl:


#! /usr/bin/perl
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use DB_File;
use Fcntl;

print "Content-Type:text/html\n\n";

my %colors;
my $dbfile="/home/users/web/b362/ipw.typomaniac101/public_html/cgi-bin/colors.txt ";

# tie %colors hash to db file in color.txt
tie (%colors"DB_File"$dbfileor die "Can't open $dbfile$!\n";

# access db in colors.txt by accessing keys in %colors hash as follows
print "$colors{'ffffff'}";
print "$colors{'000000'}";
print "$colors{'ff0000'}";

untie %colors;



and then run the scripts in that order - they should show the same results.

typomaniac



 
Msg#: 4326841 posted 12:44 am on Jun 23, 2011 (gmt 0)

Problem solved and many thanx. An interesting phenomina taking place here. When the text file is created with the script it reads them just fine but if the text file is appended without the scipt its a no go.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved