Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: I LOVE PLACK!!!!

by Logicus
on Sep 03, 2011 at 00:37 UTC ( #923954=note: print w/ replies, xml ) Need Help??


in reply to Re^2: I LOVE PLACK!!!!
in thread I LOVE PLACK!!!!

I would assume that Plack is amazing to you

Well yeah... sure some of you got into that years ago probably but the future Perl community, us, the noobs, are still finding it and going wow...

Ok I'll put some comments in there...

listing of /var/www/action.psgi ------------------------------- use UNIVERSAL::require; ## # awesome peice of kit lets you do stuff like; # # my $module = "foo::bar"; # my $module->use(); # use Plack::Request; ## # # gives you alsorts of useful information on a silver # platter. # my $app = sub { ## # plack compiles the whole program into a subroutine, so # you gotta hand it a code ref with a specific format to # its return # local $_; # # give us our very own private temporary "$_" # pronounced "it" # $_->{'env'} = shift; $_->{'req'} = Plack::Request->new($_->{'env'}); $_->{'qd'} = $_->{'req'}->parameters->mixed; # # it's environment = shift # it's request = a new Plack request object that has # been handed a copy of "it's" environment # it's qd = all the query parameter pairs for it's request $_->{'qd'}->{'action'} ||= 'default'; # # make sure it's qd => action has a value or else set to # 'default' # $_->{'qd'}->{'action'} =~ s/\//\:\:/gs; # # convert any forward slashes to double colons # in it's qd => action # $_->{'qd'}->{'action'} = "actions::$_->{'qd'}->{'action'}"; # it's qd => action = "action::" and it's qd => action # # (as in concatenate "action" and the value of # it's qd action) # if ($_->{'qd'}->{'action'}->use()) { # # test if we are able to use the module name # for instance if no action=value pair is given # in the query data, then the action to be called # will default to "actions::default". # # if we have the module ("ie /actions/default.pm") # then call it's method called "render" to produce # the return value. return [ 200, ['Content-Type' => 'text/html'], [$_->{'qd'}->{'action'}->render()] ]; } else { #else the module doesn't exist, either it's been deleted #or they have typed the url wrong etc... return [ 404, [ 'Content-Type' => 'text/plain' ], [ 'Error cannot find requested page'] ]; } }; #Simples!!

With that you can put .pm files in a file/directory structure such that a request for action=foo/bar/baz will automatically load the module baz.pm in the folder foo/bar

That is such a neat little setup!

The simplest action module would then go something like :

sub render { "hello world" } 1;

By using "it" in the PSGI file instead of "my" variables, the values remain available to the action modules thus :

sub render { " hello user from $_->{'env'}->{'REMOTE_ADDR'}" } 1;


Comment on Re^3: I LOVE PLACK!!!!
Select or Download Code
Re^4: I LOVE PLACK!!!!
by davido (Archbishop) on Sep 03, 2011 at 02:25 UTC

    Now just for fun install Mojolicious, and play around with Mojolicious::Lite. It can sit on top of Plack. Don't worry about its templating; it provides a means of layering in whatever system you prefer. Just see how much fun things like the stash and routes are.

    I don't guarantee you will like it. But I do guarantee it will be interesting to fiddle with for awhile.

    From the docs:

    # Using Mojolicious::Lite will enable "strict" and "warnings" use Mojolicious::Lite; # Route with placeholder get '/:foo' => sub { my $self = shift; my $foo = $self->param('foo'); $self->render(text => "Hello from $foo!"); }; # Start the Mojolicious command system app->start;

    It's not for everyone. And it's maybe not as flexible as Catalyst. But for rapid development of a web application it's fun.

    ...and as I mentioned, it can use Plack as its back-end, which means you can still have the Plack love.


    Dave

      Yeh, I had a play with mojolicious::lite a few weeks ago, just before I wrote Slang. It seems pretty funky, but one question I had was what if there are dozens or hundreds of routes?

      Typically the way I organise things my routes are laid out using the filesystem;

      default
      usercp
      usercp/password
      usercp/password/forgotten
      usercp/password/reset
      etc...

      It appears that under mojo you have to create a route in the program for each one and I can see that becoming quite unwieldy quite quickly for the size of apps I tend to build.

      Is there a way around that? I haven't looked into it enough to be sure.

        Routes can have wildcards at any position. So userca/password and usercb/password and usercd/password could all be one route with different users. What you do with the portion of the route that fills the wildcard is up to you. You could, for example, let your program logic deal with wildcards at the /forgotten and /reset level, or you could let routes send them to different models. In other words, you do have control over whether the routes lead to a destination or to logic. I'm sure I'm not being clear (have a cold that seems to put my head in a fog), but look at Mojolicious::Guides::Routing for examples.


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://923954]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-12-29 02:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls