Hi all,

a while ago i wrote Mojolicious::Lite: Should i use the build-in web server?.

In the last months I didn't have the time to dive a bit deeper into this stuff.

But anyway, one must start somehow, regardless how poor the first approach might be.

Here is what did:

Nose.pm

package Nose; use XML::LibXML; use Moo::Role; my $xml = sub { my ( $self, $params ) = @_; # you really need $self my $doc = XML::LibXML::Document->new( '1.0', 'utf-8' ); my $root = $doc->createElement("myapp"); my $tag = $doc->createElement("nose"); my $value = $params->{"nose"}; $tag->appendTextNode($value); $root->appendChild($tag); $doc->setDocumentElement($root); $doc->toString(); }; sub nose { my ( $self, $params ) = @_; $self->$xml($params); } 1; __END__

Karl.pm

package Karl; use Moo; with qw(Nose); 1; __END__

myapp.psgi

#!/usr/bin/env perl use Mojolicious::Lite; use Karl; my $moo = Karl->new(); get '/foo' => sub { my $c = shift; my $params->{nose} = $c->param('nose'); $c->render( text => $moo->nose($params) ); }; app->start; __END__

run.pl

#!/usr/bin/env perl use strict; use warnings; use Mojo::Server::Hypnotoad; my $app = q(/Users/karl/Documents/workspace/monks/MyMoo/MyMojo/myapp.p +sgi); my $hypnotoad = Mojo::Server::Hypnotoad->new; $hypnotoad->run($app) __END__

For what this stuff is (or should be) good for please see ibidem. I didn't want to quote it because it is a bit extensive. Sorry for the inconvenience.

Basically it does why i want. I tried to simplify it as much i could (skipping the database part a.s.o.).

But this approach seems to be so simple that i'm in doubt if i miss something.

Thank you very much for any hint.

Update: The database part that i skipped for simplification:

Connector.pm

package Connector; use DBIx::Connector; use Types::Standard qw(InstanceOf); use Moo::Role; has 'connector' => ( is => 'ro', builder => '_build_connector', handles => [qw(dbh txn)], isa => InstanceOf( ['DBIx::Connector'] ), ); sub _build_connector { my $dsn = qq(dbi:SQLite:dbname=db/myDB.sqlite); my $conn = DBIx::Connector->new( $dsn, { RaiseError => 1, AutoCommit => 1, } ); $conn->mode('fixup'); $conn; } 1; __END__

MyApp.pm

package MyApp; use Moo; with qw(Connector); 1; __END__

myapp.pl

use Mojolicious::Lite; use MyApp; my $moo = MyApp->new(); get '/foo' => sub { my $c = shift; my $query = qq(select nose from myTable); my $dbh = $moo->dbh; my $sth = $moo->txn( sub { my $sth = $dbh->prepare($query); $sth->execute; $sth; } ); my $row = $sth->fetch; $c->render( text => "<h1>$row->[0]</h1>" ); }; get '/bar' => sub { my $c = shift; my $query = qq(select monk from myTable); my $dbh = $moo->dbh; my $sth = $moo->txn( sub { my $sth = $dbh->prepare($query); $sth->execute; $sth; } ); my $row = $sth->fetch; $c->render( text => "<h1>$row->[0]</h1>" ); }; app->start; __END__

I wasn't aware of this prefork issue but it seems like my example works as designed with hypnotoad ;-)

Edit: Added comment about $self.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»


In reply to RFC: Mojolicious::Lite and Moo: A Very Basic Application Layout by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.