here is another cool solution, based upon the original skeleton.  the goal 
is to have a file, say `main/index.pl` be writeable as

#!/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>
"


and we can get all the goodies, with hidden scaffolding of the underlying 
perl templating code (could add Exporter, etc.).  it solves (almost) all of 
my problems and then some:

1. prints something to the world.
2. shows session management *--- except why does it increase the counter by 
2 on every invokation??*
3. parameter access via $req->parameters->{'q'} and  $req->path_info
4. the ability to redirect --- could be specially parsed or codable.
5. scaffolding all elsewhere where I no longer have to worry about it.

the "and then some" is huge.  MINIMAL CODE FOR MINIMAL PURPOSES.  my code 
is now lightweight, just as CGI, but I can get the goodies of, say, 
plackup.  of course, I have a lot more to learn here, but this is a great 
start. sorry, tatsuhiko, but I think something like this ( of course more 
professionally written than what I can do in my first day with plack ) is 
ideal for a lightweight low-learning curve approach to web programming.

I think I prefer this solution to Plack::App::PSGIBin because (1) the code 
in each page is lighter-weight; (2) the docs on Plack::App:PSGIBin lack 
working examples.

#!/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";

builder { 
  mount "/static" => builder { 
    enable 'ConditionalGET'; 
    enable 'ETag', file_etag => [qw/inode mtime size/],  ; 
    enable 'ContentLength'; 
    Plack::App::File->new(root => "static")->to_app; 
  }; 

  mount "/" => builder { 
    enable 'Session', store => Plack::Session::Store::File->new( dir => 
'session' ); 
    $app; 
  };
};


-- 

--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to