APvanKampen has asked for the wisdom of the Perl Monks concerning the following question:

The code below was giving a null string with CGI::Ajax-0.681 I am using modperl1 and apache1. I did get a patch from Brian Thomas.

--- lib/CGI/Ajax.pm (revision 95) +++ lib/CGI/Ajax.pm (working copy) @@ -996,8 +996,9 @@ return("") if not defined $func_name; return("") if $func_name eq ""; my $rv = ""; - my $script = $0; - $script =~ s/.*[\/|\\](.+)$/$1/; + my $script = $ENV{'REQUEST_URI'}; my $outside_url = $self->url_list()->{ $func_name }; my $url = defined $outside_url ? $outside_url : $script; if ($url =~ /\?/) { $url.='&'; } else {$url.='?'}

Then make the following change to your script. Change this line:

print $pjx->build_html( $cgi, &Show_HTML($env),$env );
to this...
print $pjx->build_html( $cgi, \&Show_HTML($env));

Actually, to pass the sub by ref I have to do this:

my $ref_show = \&Show_HTML; my $show = $ref_show->($env);

However, I noticed this on another occassion, so it has probably nothing to do with the ajax module.

So here is the working code. HOWEVER.. Brian Thomas noticed a couple of '1's at the beginning, which I do not see. So for some things the mystoury continues...

<Location /pjx> SetHandler perl-script PerlInitHandler Apache::StatINC PerlHandler Test::pjx PerlSetVar DBASE ** PerlSetVar DBUSER ** PerlSetVar DBPASS ** </Location>
package Test::pjx; use strict; use Apache::Constants qw(:common); use CGI qw(:all); # or any other CGI:: form handler/decoder use CGI::Ajax; use warnings; use diagnostics; $^W=1; local $SIG{__WARN__} = \&Carp::cluck; sub handler { my $r = shift; my $env = $r->subprocess_env; my $cgi = new CGI; my $pjx = new CGI::Ajax( 'exported_func' => \&perl_func ); my $ref_show = \&Show_HTML; my $show = $ref_show->($env); print $pjx->DEBUG(1); print $pjx->JSDEBUG(1); print $pjx->build_html( $cgi, $show); return OK; } sub perl_func { my $input = shift; # do something with $input my $output = $input . " was the input!"; return $output ; } sub Show_HTML { my $env = shift; my $html = <<EOHTML; <HTML> <BODY > Enter something: <input type="text" name="val1" id="val1" onkeyup="exported_func( ['val1'], ['resultdiv'] );" > <br> <div id="resultdiv" ></div> </BODY> </HTML> EOHTML return $html; } 1; __END__
Screenoutput: http://simulator.bla.nl:8080/pjx Enter something: arnold4 was the input! /pjx?fname=exported_func&args=arnold4&val1=arnold4 ^^^^^ Used to be null, so correct now

Replies are listed 'Best First'.
Re: CGI::Ajax gives 'null' scriptname
by merlyn (Sage) on Dec 13, 2005 at 23:13 UTC
    I'm guessing that CGI::Ajax wants to run as CGI, not as a mod_perl handler, and therefore it's fetching things like "script name" from the environment. You'll probably have to set up the "subprocess_env" to make it work, and that's different between mod_perl1 and mod_perl2, so you'll have to elaborate to say which mod_perl you have.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Quite the opposite, as I read it the documentation is "all about running it under mod_perl".
      We have added support for other CGI handler/decoder modules, like CGI::Simple or CGI::Minimal, but we can't test these since we run mod_perl2 only here.
      For the grandparent, have you tried contacting the authors? They're quite helpful. (It's such a new module few people have much if any experience with it)

      --
      In Bob We Trust, All Others Bring Data.

        I Checked out Brian Thomas. As you can see, he came up with a patch. Maybe notice the problem with passing sub by ref.. I had to make some detour, and I am still not quite sure what I am pasing .. Arnold van Kampen
        my $ref_show = \&Show_HTML; my $show = $ref_show->($env); print $pjx->build_html( $cgi, $show);

        In stead of

        <code> print $pjx->build_html( $cgi, \$Show_HTML($env) );