Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Accessing DB

by Anonymous Monk
on Oct 05, 2010 at 21:08 UTC ( #863683=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

This is what I want to do:

1. set cookies
2. get cookies
3. get MySQL data based on cookie (i.e. SELECT name WHERE id=$cookie) using a config file for the username and password
4. set a new cookie using MySQL data

So if anyone knows of a script that will do this will you please let me know.

I have managed to set/get the cookies but I cannot seem to access the database. I've tried every script --below is he latest. I keep getting this error: did not return a true value at myapp.cgi line 10. BEGIN failed--compilation aborted at myapp.cgi line 10.

#!/usr/bin/perl -w -T my $HOME_DIR; BEGIN { '/home/somename/public_html/pc'; } use strict; use lib '/home/somename/public_html/pc/modules'; # this is where I pu +t use lib '/home/somename/perl/usr/lib/perl5/site_perl/5.8.8'; # this is + where I installed Perl modules use CGI::Carp qw(fatalsToBrowser); use MyApp; MyApp->new( ,-config_file => '/home/somename/public_html/pc/config/pc.cfg' )->run;
user the_username password the_password dsn dbi:mysql:database=somename_database:host=localhost cgi_session_dsn driver:mysql;serializer:Storable cookie_path /
package MyApp; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::AutoRunmode; use CGI::Application::Plugin::Config::Simple; use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); use CGI::Application::Plugin::Session; sub cgiapp_init { my ($self, %params) = @_; # -- config $self->config_file($params{-config_file}); $self->dbh_config( $self->config_param('dsn') ,$self->config_param('user') ,$self->config_param('password') ); # -- session $self->session_config( CGI_SESSION_OPTIONS => [ $self->config_param('cgi_session_dsn') ,$self->query ,{Handle=>$self->dbh} ], DEFAULT_EXPIRY => '+31d', # should this match -expires b +elow? TBD COOKIE_PARAMS => { -expires => '+31d' ,-path => $self->config_param('cookie_path') } ); } sub terminate { my $self = shift; $self->session->flush if $self->session_loaded; # CGI::Sesssion documentation says "auto-flushing can be unreliable +. # ...regard it as mandatory that sessions always need to be explici +tly # flushed before the program exits... sub teardown() would be the # appropriate place to do this. } sub display_form : StartRunmode { my $self = shift; my $q = $self->query; my $session_data = $self->session->param('stuff') || 1; my $result = $q->start_html(-title => 'Client Login') . $q->start_form . 'First Name: ' . $q->textfield(-name=>'firstName') . $q->br . $q->submit . $q->hidden(-name =>'rm', -value => 'page_2') . $q->end_form . $q->br . $session_data . $q->end_html ; $session_data++; # reload the page to see the value change above $self->session->param(stuff=>$session_data); return $result; }
Please help!

Replies are listed 'Best First'.
Re: Accessing DB
by marto (Archbishop) on Oct 05, 2010 at 21:20 UTC

    Did you download this from somewhere? Add 1; as your last line of the module. You should read the CGI::Application documentation, this is covered there.

      Thank you. Yes, I found it online but it seems to be plagued with problems.
Re: Accessing DB
by mjscott2702 (Pilgrim) on Oct 06, 2010 at 07:56 UTC
    As previously stated, all modules should return a true value - from the perlmod man page:
    1; # don't forget to return a true value from the file
    Definitely read up on the modules you are using e.g. perldoc CGI::Application - if the module you downloaded has this basic error, I would be nervous about using it for anything....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://863683]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2018-04-26 11:49 GMT
Find Nodes?
    Voting Booth?