Mod_perl2, apache2.2, and CGI::Ajax notes…

Following along in my mod_perl2 notes, I wanted to document how to get CGI::Ajax working with mod_perl2. I hit a couple of snags along the way that are worth noting. First, the generated javascript for my functions was calling httpd? + vars, rather than my URI /modperl_handler/ajax? + vars. This was frustrating, but I determined that it was grabbing httpd from $0, so I changed it locally and the script worked after that. The second snag I hit was because I was instantiating my CGImodule globally instead of locally, and I would get segfaults now and then. Instantiating it inside the handler was the right way to go. Here is a working example:

package AjaxTest;

use CGI;
use CGI::Ajax;

use Apache2::RequestRec();
use Apache2::RequestIO();
use Apache2::Const -compile => qw(OK);

sub handler {
    my ($r) = @_;

    my $cgi = new CGI;  # had this outside the handler and was getting segfaults

    # Have to redefine $0 for CGI::Ajax because it's used to call further URLs from
    # javascript ajax functions.  (otherwise it did "httpd?"...)
    local $0 = $ENV{"REQUEST_URI"};
    $0 =~ s/?.*//;

    # Start Ajax stuff
    my $pjx = new CGI::Ajax("test_ajax" => &test_ajax);
    # Don't compress javascript (1 for user fcns only, 2 for all)
    $pjx->JSDEBUG(1);
    # Send stderr to web logs
    $pjx->DEBUG(1);

    print $pjx->build_html( $cgi, &base_page);
    return Apache2::Const::OK;
}

sub base_page {
    return "Ajax mod_perl testmod_perl 2.0.2 on apache 2.2.2 rocks! <p><div id="test">Change me</div><p>nn";
}

sub test_ajax {
    my $time = time();
    return "Test successful; $time<p>";
}

1;

Wireless Network Upgrade

I recently bought the Linksys pre-N router (WRT300N) and a laptop PC card. I hate to say it, but the 4x range that it promises is flat out wrong. I get better connection to my wireless G router (WRT54G) up on the second floor. However, the throughput is amazing – 270 mbit/sec as opposed to 54. I just need to get it working well.

I see that they have come out with some firmware upgrades since I last checked a week ago, and it mentions improving wireless power and performance. We’ll see what happens!

Laptop Woes

Sometimes you just can’t win. Soon I am going on a 12 day trip where I will need my laptop, and I have three to choose from:

  • The first one is an HP that has a power cord that started fraying at the connection. If I adjust it just so and don’t move, then I can get it to charge the battery and run the computer. Additionally, sometimes it overheats when I run a certain application (read: games) and just powers itself off hard.
  • The second one my wife used to use, but she dropped it out of her lap last summer and the headphone and microphone jacks got destroyed when the cords tugged on the unit. The power adapter is angled now as well, but at least it works.
  • The third one got its screen cracked, and I use it with an external monitor. Not good for trips, though.

I guess the best thing to do is go get a USB headphone with boom microphone that I can plug into the second one. Not the best solution (my wife has one and it is not very reliable), but short of getting another laptop, probably the right thing to do.

I don’t want to get a new laptop until one comes with wireless-N as part of the package. Hence, I wait and play old games like Diablo 2 when I am not working.

Mod_perl 2, apache 2.2, perl handler example

  1. Make sure that mod_perl.pm is being loaded on server start-up! In httpd.conf, have a line like:
    LoadModule perl_module modules/mod_perl.so
  2. The perl that runs with the apache server needs to know where you are putting your stuff, along with any other perl-isms at startup, so we set a perl file that gets sourced by apache/perl when loading. In httpd.conf, add:
    PerlRequire /path/to/your/perl_startup.pl

    This file itself, can set several things, but at a minimum adds the module path for your perl modules to be referenced in:

    use lib qw(/path/to/your/perl_modules);
    1;
  3. Now set up code for each specific handler! For each mod_perl app you put out there, you need to map a location to the perl module, in httpd.conf, like so:
    <Location /modperl_rocks>
           SetHandler perl-script
           PerlResponseHandler MyApache2::Rocks
    </Location>
  4. Next the perl code itself! In the perl lib directory defined above, /path/to/your/perl_modules, create a file MyApache2/Rocks.pmand have it look something like:
    package MyApache2::Rocks;
    
    use strict;
    use warnings;
    
    use Apache2::RequestRec();
    use Apache2::RequestIO();
    use Apache2::Const -compile => qw(OK);
    
    sub handler {
        my ($r) = @_;
        $r->content_type('text/html');
        print "mod_perl 2.0.2 on apache 2.2.2 rocks!n";
        return Apache2::Const::OK;
    }
    
    1;
  5. Finally, stop and restart apache, and for this example surf to /modperl_rocks. All should work well…

Sam Adams Cherry Wheat

I thought I would start keeping track of the different kinds of beer that I have bought and liked. This probably belongs in a wiki or something else, but for now it gets dumped in the blog, since that’s already set up.

I got Sam Adams Cherry Wheat last night and tried a bottle before moving on to Shiner Kolsch. It was good – not too wheaty, which before I got to like wheat beers was a turn-off, and not too sweet. It left a cherry-like aftertaste that was kind of nice. I don’t know if I would drink a whole lot at once, but it’s good for a beer flight or something like that. I have five more waiting for me, but from the one I had last night, it’s a good change from the ordinary and I would buy it again.