This isn't verbatim what I'm doing, since posting a webapp in here doesn't really make sense. But I'll try to be true to the problem I'm trying to solve. Please forgive me any typos.

What I would like to do is use CGI::Application::Plugin::DBH to provide access to my database in conjunction with CGI::Ajax. When the page loads normally, the CGI::Application setup sub is called and Plugin::DBH can do its thing. However, when 'BUILD_CHART' is called from the the javascript function 'GET_CHART', it doesn't go through the setup routine. Can I still access the database config set up Plugin::DBH when executing a the 'BUILD_CHART' sub? How?

package MyWebApp; use base 'CGI::Application'; use strict; use CGI; use CGI::Ajax; use CGI::Carp qw(fatalsToBrowser); use CGI::Application::Plugin::DBH qw(dbh_config dbh); use DBI qw(:sql_types); use DBIx::Chart; sub setup{ my $self = shift; $self->start_mode('mode1'); $self->mode_param('rm'); $self->run_modes( 'mode1' => 'runMode1', 'mode2' => 'runMode2' ); $self->dbh_config('dbh1', ['DBI:DB2:DS1', 'usr', 'pwd', {RaiseError +=> 0, PrintError => 0}]); } sub runMode1 { my $q = new CGI; my $pjx = new CGI::Ajax('GET_CHART' => \&BUILD_CHART, 'skip_header' +=> 1); open(SRC, "pageSrc.htm") or die $!; my $src = <SRC>; close(SRC); my $output = $q->start_html; $output .= $src; $output .= $q->end_html; return $pjx->build_html($q, $output); } sub BUILD_CHART { my ($chartSelection, $screenWidth) = @_; my $chartWidth = $screenWidth / 2; #this is where I want to take advantage of CGI::Application::Plugin: +:DBH -- so that I don't have to build this connection every time an a +jax call is made to this subroutine my $dbh = DBIx::Chart->connect('DBI:DB2:DS1', 'usr', 'pwd', {RaiseEr +ror => 0, PrintError => 0}) or die $DBI::errstr; my $qryStatement = "SELECT SOMETHING FROM SOMEWHERE"; my $chartStatement = "RETURNING BARCHART(*), IMAGEMAP WHERE ..."; my $sth = $dbh->prepare("$qryStatement $chartStatement"); unless(eval{$sth->execute();}){ die "Failed to execute!"; } my $tempChartRef = $sth->fetchrow_arrayref or die "Failed to fetch!" +; $sth->finish; my $imageData = $$tempChartRef[0]; my $imageMap = $$tempChartRef[1]; my $imageName = &WRITE_IMAGE_TO_DISK($imageData, 'PNG', 'barchart'); my $html = "<IMG SRC=\"/$imageName\" USEMAP=\"#barMap\">"; $html .= $imageMap; return $html; }

In reply to Using CGI::Ajax and CGI::Application::Plugin::DBH by jrsimmon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.