- Patrick Donelan writes a good introduction for PSGI and Plack from the web application developers point of view.
I am thinking about writing a new web program in PSGI/Plack. I purchased the 24-chapter book, but it seems geared towards porting over existing apps, not as a starter tutorial.as I was looking for more info, I noticed the following is dead on the plack webpage:
- Patrick Donelan writes a good introduction for PSGI and Plack from the web application developers point of view.
- but did not see an email for the maintainer.
- nice starters are in http://search.cpan.org/~leejo/CGI-Alternatives-0.14/lib/CGI/Alternatives.pm and http://savage.net.au/Perl/html/plack.for.beginners.html .
- so I really need basic tutorial examples / documentation for a new app. specifically, I need
- hello world (documentation already exists). I like the simplicity of keeping each webpage in its own .pl file. Less enthused by mojo like approach of one big dispatch app. but I could live with it. is one dispatch app required for psgi/plack (http://advent.plackperl.org/2009/12/day-12-maps-multiple-apps-with-mount-and-urlmap.html)?
- session management (aka CGI:Session) -- aka php $_SESSION
- GET, POST, SERVER read access -- aka php $_GET $_POST $_SERVER
- http redirect -- aka php header("Location: /elsewhere/file.thml");
- CGI::Carp fatalstobrowser -- aka php default
I have been using Text::Template for a while, and it seems fine. but it could be easily switched.that's it.probably 50 lines of code. would it be possible for someone in the know to post here and on the plack/psgi website, too?help?
--
---
You received this message because you are subscribed to the Google Groups "psgi-plack" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psgi-plack+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Undefined subroutine &Plack::Sandbox::_2fUsers_2fme_2testsite_2fwww_2fplack_2epl::doIndex called at /Users/me/testsite/www/plack.pl line 44. at /Users/ivo/syllabus.space/www/phpi/plack.pl line 43
sub doIndex {
return [ '200', [ 'Content-Type' => 'text/html' ], [ 42 ], ];
}
and then requested localhost:5000/main . I guess I can now replace the %funcs call `my proc = $funcs{$args[1]}` with something like
args[0] =~ s/\.//g; ## ignore all attempts to traverse up the hierarchy or use hidden files (-e "/$DOCUMENTROOT$args[0]") or return [ 404, [ 'Content-Type' => 'text/html; charset=utf-8' ], [ "404 file $args[0] not found" ] ];
my $perlcode= slurp("/$DOCUMENTROOT/$args[0]"); ## could check that the first line contains perl magic
my @rv = eval perlcode;
($@) and die "in nice html, say your perlcode in $args[0] died";
($#rv != 2) and die "in nice html, say we want three args back from $args[0]";
return @rv;
known-to-work starter examples (each showing off a different feature) would be a great help on the Plack http://plackperl.org/ website. alas, I don't know tatsuhikos' email (who I am assuming maintains it).
thanks again, andy.
regards,
/iaw
--
---
You received this message because you are subscribed to the Google Groups "psgi-plack" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psgi-plack+...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to psgi-plack+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
---
You received this message because you are subscribed to a topic in the Google Groups "psgi-plack" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psgi-plack/0tS-oI6B__0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psgi-plack+unsubscribe@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to psgi-plack+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
---
You received this message because you are subscribed to a topic in the Google Groups "psgi-plack" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/psgi-plack/0tS-oI6B__0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to psgi-plack+...@googlegroups.com.
--
---
You received this message because you are subscribed to the Google Groups "psgi-plack" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psgi-plack+...@googlegroups.com.
#!/usr/bin/perl placked code
use strict; ## irrelevant, but shows flexibility;
"
<html>
<body style=\"background-color:cyan\">
<h1>hello</h1>
<p>Our session counter is ".($req->session->{counter})."</p>
<p>We have had good results with this perl file named <tt>".$perlfilename."</tt>.</p>
</body>
</html>
"
#!/usr/bin/perl -w
use warnings FATAL => qw{ uninitialized }; ## or use warnings FATAL => ':all';
use common::sense;
use Plack::Builder;
use Plack::Request;
use Plack::App::File;
use Plack::Session::Store::File;
my $CODEROOT = "./";
my $htmlcontent= [ 'Content-Type' => 'text/html; charset=utf-8' ]; ## standard default
my $app = sub {
my $env = shift; # PSGI env
my $req = Plack::Request->new($env);
($req->session) or return [ 404, [ 'Content-Type' => 'text/html; charset=utf-8' ], [ '404 session not found' ] ];
$req->session->{counter}++;
(defined($req->session->{auth})) or $req->session->{auth} = 0;
my $uri = lc($req->path_info); ## case-insensitive for windows
$uri =~ s#^/##o; ## Remove the first slash
(defined($uri)) or $uri = 'index.pl';
use Cwd;
while ($uri =~ s/[^a-z]\.//g) { } ## ignore all replace, e.g., /./ab or /.ab, but not /asd.cd/as
sub findfile {
foreach (@_) { ((-r "$CODEROOT/$_") && (-f "$CODEROOT/$_")) and return "$CODEROOT/$_"; }
return undef;
}
my $perlfilename = findfile( "$uri", "$uri.pl", "$uri/index", "$uri/index.pl" );
my $fourofour="404 file '$uri'=>'$perlfilename' definitely not found in ".getcwd()
." with ".($perlfilename||"")."<p>check that your permissions are set correctly";
## ."<pre backgroundcolor=\"orange\">". glob("*")."</pre>"; ## for further checking
(defined($perlfilename)) or return [ 404, $htmlcontent, [ $fourofour ] ];
use Perl6::Slurp;
my $perlcode= slurp($perlfilename); ## could check that the first line contains perl magic
($perlcode) or return [ 404, $htmlcontent, "no content in $perlfilename" ];
# perl template code...nice...very nice...
my @rv = eval "#!/usr/bin/perl -w\nuse common::sense;\nuse warnings FATAL => qw{ uninitialized };\n$perlcode";
($@) and return [ 404, $htmlcontent, "<p>your perlcode in $uri died with error <b>$@</b></p>" ];
($#rv < 0) and return [ 404, $htmlcontent, "<p>your perlcode in $uri must return at least one (html string) argument" ];
($#rv < 1) and return [ 200, $htmlcontent, [ $rv[0] ] ]; ## common html return
($#rv < 2) and return [ 200, $rv[0], [ $rv[1] ] ]; ## can this do redirect?
return @rv;
};
((-d 'session') && (-d 'template')) or die "please have writable session and template directories or change location";
the answer is that plackup requests not only the file, but also favicon.ico. thus, it is called twice for each request, and the counter increases by 2.