tweetscale progress

lilstevey on 2009-11-24T19:49:18

I started having a play over a week ago but haven't had the time to do much - I'll blog this and see if I get any time to go further.

This entry is brought to you by use v6; - a delightful experience.

Perl v6.0.0 required--this is only v5.8.9, stopped at testserver.pl line 1.

I've been doing statically typedish language for too long. Theres me leaping into introspection, when, for now, for the serverside of things, a simple bit of dynamic programming will suffice. Lets quickly knock out a simple server.

testclient.pl
use v6;
use Server;
use TestClass;
use TestRole;

my $tc = TestClass.new();
my $r = TestRole;
my $a = Server.new( clzz => $tc, rle => $r );

$a.startServer();
Server.pm
use v6;
use HTTP::Daemon;

class Server
{
   has $clzz;
   has $rle;

   method startServer() {
      my HTTP::Daemon $d .= new;
      say "see {$d.url}";
      while my HTTP::Daemon::ClientConn $c = $d.accept {
         while my HTTP::Request $r = $c.get_request {
            my $p = $r.url.path;
            my @p = split "\/",$p;
            my $m = pop @p;

            if ( $r.method eq 'GET' && $rle.can( $m ) ) {
               $c.send_response($clzz."$m"());
            } else {
               $c.send_error('RC_FORBIDDEN');
            }
            warn "{$r.method} {$r.url.path}";
         }
      }
   }
}

Thats as simple as $rle.can( $m ) && $c.send_response($clzz."$m"()) - though my first attempt was $clzz.$m() - which resulted in "invoke() not implemented in class 'Perl6Str'"

Now to see what can be done on the client...


Nice!

JonathanWorthington on 2009-11-25T01:08:58

Nice post. Just so you know, $x.$meth(...) is fine if $meth is actually a code reference rather than a method name. If you have a name, then you always need the quotes and to interpolate it. If you'd somehow got hold of a method itself (for example through .WALK or by introspection) then you could use the $x.$meth form though. They're distinguished syntactically (quote vs variable). So the error was just telling you that a string isn't something that can be invoked.

Hope this helps!

Jonathan