You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by silent <si...@gmail.com> on 2006/11/24 09:47:21 UTC

why can't I get data from DBM in mod_perl

Hi all,
I need help with mod_perl:
My ENV is redhat ES4 + apache2.0.59 + mod_perl2.

I wrote a small script:
####################
package myconf;

use strict;
use warnings;
use DB_File;
use Fcntl;

use Apache2::Const -compile => qw(:common);
use Apache2::Request ();
use Apache2::Connection ();
use Apache2::URI ();

my $db = "/www/data/myconf.db";
tie my %dbh,'DB_File',$db || die "can not open database\n";
my @title = keys(%dbh);

warn "data count:$#title\n";  ### but in error_log  it is -1

sub handler{
        my $r = shift;
        $r->content_type('text/plain');

        my $req = Apache2::Request->new($r);
        my $file = $req->param('file') || "";
        if (defined $file and exists $dbh{$file}) {
                print $dbh{$file};
                return Apache2::Const::OK;
        }else{
                for (0..$#title){
                        print "$title[$_]<br>\n";
                }
        }
}
1;

__END__

If I run it under command line I will got the data number of db is 41, but
if in apache ,it is -1, and open not failed.

Why?
Thanks!

Sil3nt.


Re: why can't I get data from DBM in mod_perl

Posted by to...@tuxteam.de.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Nov 24, 2006 at 11:57:58AM +0100, D. Bolliger wrote:
> tomas@tuxteam.de am Freitag, 24. November 2006 11:27:
> 
> [snipped]
> > Watch this:
> >  | tomas@herr-turtur:~$ perl
> >  | use DB_File;
> >  |
> >  | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
> >  | $dbh{'one'}=1;
> >  | $dbh{'two'}=2;
> [snipped]
> 
> Hello Tomás
> 
> This won't die if the tie failed, because of the 
> operator precedences in this statement.
> 
> Either use 
> 
> tie (my %dbh, 'DB_File', 'foobar') || die "Argh: $@";
> 
> or 
> 
> tie my %dbh, 'DB_File', 'foobar' or die "Argh: $@";

Yep, you are quite right. Mindless cut&past on my part. Thanks.

regards

- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFZ9THBcgs9XrR2kYRAmS0AJ9Yu1GBr6hTaKek9SNgaSy1JtIOxwCfXbBw
2RrHVfIxz8tWT1Y+J7gngFU=
=zwnB
-----END PGP SIGNATURE-----


Re: why can't I get data from DBM in mod_perl

Posted by "D. Bolliger" <in...@dbolliger.ch>.
tomas@tuxteam.de am Freitag, 24. November 2006 11:27:

[snipped]
> Watch this:
>  | tomas@herr-turtur:~$ perl
>  | use DB_File;
>  |
>  | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
>  | $dbh{'one'}=1;
>  | $dbh{'two'}=2;
[snipped]

Hello Tomás

This won't die if the tie failed, because of the 
operator precedences in this statement.

Either use 

tie (my %dbh, 'DB_File', 'foobar') || die "Argh: $@";

or 

tie my %dbh, 'DB_File', 'foobar' or die "Argh: $@";

Dani

答复: why can't I get data from DBM in mod_perl

Posted by silent <si...@gmail.com>.
Thanks all, after I chown it to apache user and group it works!
And I also made operator precedences mistake in my script.


Thanks! 

-----邮件原件-----
发件人: tomas@tuxteam.de [mailto:tomas@tuxteam.de] 
发送时间: Friday, November 24, 2006 6:27 PM
收件人: silent
抄送: modperl@perl.apache.org
主题: Re: why can't I get data from DBM in mod_perl

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Nov 24, 2006 at 04:47:21PM +0800, silent wrote:
> Hi all,
> I need help with mod_perl:
> My ENV is redhat ES4 + apache2.0.59 + mod_perl2.
> 
> I wrote a small script:
> ####################
> package myconf;
> 
> use strict;
> use warnings;
> use DB_File;
[...]
> warn "data count:$#title\n";  ### but in error_log  it is -1
[..]

Seems the tie didn't work and now your hash is empty.

This ain't a modperl question :-)

You'll get exactly this behaviour when you can't write to the file
(because of permissions -- or just because the file is not there).

Watch this:

 | tomas@herr-turtur:~$ perl
 | use DB_File;
 | 
 | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
 | $dbh{'one'}=1;
 | $dbh{'two'}=2;

Now foobar has some stuff in it.

 | tomas@herr-turtur:~$ sudo chown root.root foobar
 | tomas@herr-turtur:~$ ls -l foobar
 | -rw-r--r-- 1 root root 12288 2006-11-24 11:05 foobar

and now I can read (but not write) to foobar

 | tomas@herr-turtur:~$ perl
 | use DB_File;
 | 
 | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
 | print join("\n", map("$_=>" . $dbh{$_}, sort(keys(%dbh)))), "\n";
 | 

Noting... %dbh is empty!

 | tomas@herr-turtur:~$ sudo chown tomas.users foobar

Now I can open foobar r/w

 | tomas@herr-turtur:~$ perl
 | use DB_File;
 | 
 | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
 | print join("\n", map("$_=>" . $dbh{$_}, sort(keys(%dbh)))), "\n";
 | one=>1
 | two=>2

It works!

You can fine-tune the behaviour using some flags, like O_RDONLY and so
on.

HTH
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFZsj9Bcgs9XrR2kYRAvoYAJ9bq6TJd14aPjsLaQeS2JPSRC8dbgCffJEs
DiN34KDWAQgxnI0auU0CSK0=
=BlkD
-----END PGP SIGNATURE-----


Re: why can't I get data from DBM in mod_perl

Posted by to...@tuxteam.de.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Nov 24, 2006 at 04:47:21PM +0800, silent wrote:
> Hi all,
> I need help with mod_perl:
> My ENV is redhat ES4 + apache2.0.59 + mod_perl2.
> 
> I wrote a small script:
> ####################
> package myconf;
> 
> use strict;
> use warnings;
> use DB_File;
[...]
> warn "data count:$#title\n";  ### but in error_log  it is -1
[..]

Seems the tie didn't work and now your hash is empty.

This ain't a modperl question :-)

You'll get exactly this behaviour when you can't write to the file
(because of permissions -- or just because the file is not there).

Watch this:

 | tomas@herr-turtur:~$ perl
 | use DB_File;
 | 
 | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
 | $dbh{'one'}=1;
 | $dbh{'two'}=2;

Now foobar has some stuff in it.

 | tomas@herr-turtur:~$ sudo chown root.root foobar
 | tomas@herr-turtur:~$ ls -l foobar
 | -rw-r--r-- 1 root root 12288 2006-11-24 11:05 foobar

and now I can read (but not write) to foobar

 | tomas@herr-turtur:~$ perl
 | use DB_File;
 | 
 | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
 | print join("\n", map("$_=>" . $dbh{$_}, sort(keys(%dbh)))), "\n";
 | 

Noting... %dbh is empty!

 | tomas@herr-turtur:~$ sudo chown tomas.users foobar

Now I can open foobar r/w

 | tomas@herr-turtur:~$ perl
 | use DB_File;
 | 
 | tie my %dbh, 'DB_File', "foobar" || die "Argh: $@";
 | print join("\n", map("$_=>" . $dbh{$_}, sort(keys(%dbh)))), "\n";
 | one=>1
 | two=>2

It works!

You can fine-tune the behaviour using some flags, like O_RDONLY and so
on.

HTH
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFFZsj9Bcgs9XrR2kYRAvoYAJ9bq6TJd14aPjsLaQeS2JPSRC8dbgCffJEs
DiN34KDWAQgxnI0auU0CSK0=
=BlkD
-----END PGP SIGNATURE-----