2012-06-29

Perl Web Programming

It turns out it’s actually pretty easy to do web programming in Perl using PSGI once I waded through the documentation. The documentation makes things seem complex, but it’s actually pretty easy once you get down to the essentials of what you get as a request and what you need to return.

Maybe I’ll try to write up the tutorial I would have wanted to read.


Firefox Frustration

I’ve been trying out Firefox again since it seems to be the main browser by default in the Fedora repositories. However, I keep getting odd hangs while using it. I don’t know what’s inducing it – it seems to happen when I’m trying to drag tabs around.

So I guess, I’m just going have to learn how to add the repositories to get chromium, or see if I can download Google Chrome directly.


Static Web Site App in Perl

Here is the little app I wrote in Perl using PSGI (specification) to serve my website.

Currently I am running it using Starman but that may change if I get Apache up and configured for virtual hosts.

my $app = sub {

    $doc_root = "./public";
    my $env = shift;
    $path = $env->{PATH_INFO};
    $file = $path;
    $error = 0;

    $file =~ s/\.\.//g;

    if ($file =~ m(^/$)) {
        $file = "index.html";
    }

    if (-f "$doc_root/$file") {
        open FILE, "$doc_root/$file";
    } else {
        $error = 1;
    }

    if (! $error) {
        return [
            '200',
            [ 'Content-Type' => 'text/html' ],
            \*FILE,
                ]
        ;
    } else {
        return [
                '200',
                [ 'Content-Type' => 'text/plain' ],
                [ "Error - Path was; $file" ],
                ]
        ;
    }
}

Perl Web App Update

So with the above code, I no longer have to map to the exact files that I was wanting to serve. I think I’ve got security set so that only the document root and below is accessible. Howerver, I"m kind of a newbie at this, so I might have left a huge gaping hole for hackers to enjoy.


PSGI / WSGI / Rack

Having spent the day playing around with PSGI, I became curious about the alternatives (and inspirations) in the Ruby and Python space. It’s pleasing to discover that their concepts are basically the same as the Perl implementation. That’s cool, because it means I could begin programming web apps according to those specifications without having to learn a whole new framework of thinking.

That means that if I want to play around with web programming in Ruby, I can reuse most of this learning that I’m doing so far in Perl. Which is nice, because while I haven’t been a big fan of Rails, I do like Ruby, and I like PSGI, so from a brief glance at Rake, it looks like I would like it as well.

If it came down to doing web stuff in Ruby, maybe I could work on the Rake layer of web stuff, and my friends could work in Rails.


Perl Table Of Contents Script

I reworked the table of contents script from the other day to be a little less ugly. Now instead of stringing together Perl commands on the command line, the shell script calls this simple Perl script:

#!/usr/bin/env perl

@output = [];
while (<>) {
    if (/^###/) {
        $date = $ARGV;
        chomp;
        s/### //;
        push @output, qq(<a href="index.html#$date">$_</a><br>\n);
    }
}

print foreach (reverse @output);

I think this is a lot cleaner than what I had previously:

perl -lne '
  /^###/ && print "$ARGV:$_";
' * | perl -e '
@ls = <STDIN>; print foreach (reverse @ls);
' | perl -lpe '
s/### //
' | perl -lna -F: -e'
print qq(<a href="index.html#$F[0]">$F[1]</a><br>)
' > ../toc.middle

Anyway, so far I’m happy with the change.


Emacs Frustration

I’m getting annoyed with the way Emacs is mixing tabs and spaces in my writing and code. I’m going to have to go look up how to keep it from doing that again. That and fixing the broken backspace / Ctrl-H behavior are two things that I have to re-figure out each time I really start using Emacs in a particular environment.

In particular, this is a pain, because when I try to indent code so that Markdown will treat it specially, Emacs makes it all kinds of weird with mixed spaces and tabs, which then confuses Markdown as to where the lines should actually be.

So for example, the above (first) Perl snippet, the if statement should be indented (and it is visually in the Markdown source). But because it’s indented with tabs, where the surrounding lines are indented with spaces (Emacs doing, not mine), it doesn’t translate the indentation correctly to the code block.

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s