Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

I LOVE PLACK!!!!

by Logicus
on Sep 01, 2011 at 11:22 UTC ( #923617=perlmeditation: print w/ replies, xml ) Need Help??

use UNIVERSAL::require; use Plack::Request; my $app = sub { local $_; $_->{'env'} = shift; $_->{'req'} = Plack::Request->new($_->{'env'}); $_->{'qd'} = $_->{'req'}->parameters->mixed; $_->{'qd'}->{'action'} ||= 'default'; $_->{'qd'}->{'action'} =~ s/\//\:\:/gs; $_->{'qd'}->{'action'} = "actions::$_->{'qd'}->{'action'}"; if ($_->{'qd'}->{'action'}->use()) { return [ 200, ['Content-Type' => 'text/html'], [$_->{'qd'}->{'action'}->render()] ]; } else { return [ 404, [ 'Content-Type' => 'text/plain' ], [ 'Error cannot find requested page'] ]; } };

Comment on I LOVE PLACK!!!!
Download Code
Re: I LOVE PLACK!!!!
by Logicus on Sep 01, 2011 at 21:43 UTC
    QUOTE: If you've discovered something amazing about Perl that you just need to share with everyone, this is the right place. END;

    For a given definition of amazing it seems!

      I would assume that Plack is amazing to you. But if you want to share your findings with others, maybe some paragraphs of text can also explain to others why you find the snippet you posted amazing. Maybe show what you were doing before, and how Plack made it easier/clearer/simpler. Just posting code won't tell people why the code is relevant.

        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;
Re: I LOVE PLACK!!!!
by pemungkah (Priest) on Sep 02, 2011 at 20:32 UTC
    I'm really happy for you! (Tell us more!)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-07-30 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (230 votes), past polls