Permalink
Browse files

fine-tuning

  • Loading branch information...
1 parent 97087ed commit dd06ba225228174ec01d0e12d10361f7703e7277 @yanick yanick committed Jul 4, 2013
Showing with 22 additions and 50 deletions.
  1. +4 −16 examples/dancr/dancr.pl
  2. +18 −34 lib/Dancer/Tutorial.pod
@@ -17,18 +17,6 @@
set 'password' => 'password';
set 'layout' => 'main';
-sub set_flash {
- my $message = shift;
-
- flash warning => $message;
-}
-
-sub get_flash {
- my $msg = flash 'warning';
-
- return $msg;
-}
-
sub connect_db {
my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database')) or
die $DBI::errstr;
@@ -56,7 +44,7 @@ sub init_db {
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute or die $sth->errstr;
template 'show_entries.tt', {
- 'msg' => get_flash(),
+ 'msg' => flash( 'message' ),
'add_entry_url' => uri_for('/add'),
'entries' => $sth->fetchall_hashref('id'),
};
@@ -72,7 +60,7 @@ sub init_db {
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute(params->{'title'}, params->{'text'}) or die $sth->errstr;
- set_flash('New entry posted!');
+ flash message => 'New entry posted!';
redirect '/';
};
@@ -89,7 +77,7 @@ sub init_db {
}
else {
session 'logged_in' => true;
- set_flash('You are logged in.');
+ flash message => 'You are logged in.';
return redirect '/';
}
}
@@ -103,7 +91,7 @@ sub init_db {
get '/logout' => sub {
session->destroy;
- set_flash('You are logged out.');
+ flash message => 'You are logged out.';
redirect '/';
};
@@ -47,23 +47,21 @@ These all can be installed using your CPAN client, as in:
=head1 The database
-We're not going to spend a lot of time on the database, as it's not really the point of this particular tutorial.
-
-Create the database using:
+We're not going to spend a lot of time on the database, as it's not really the point of this particular
+tutorial.
+
+Create the database by running the follow from the shell:
+
+ $ cat - | sqlite3 database
+ create table if not exists entries (
+ id integer primary key autoincrement,
+ title string not null,
+ text string not null
+ );
+ ^D
- sqlite3 database
-
-Then paste in this code to create the table:
-
- create table if not exists entries (
- id integer primary key autoincrement,
- title string not null,
- text string not null
- );
-
-Use C<.exit> to when finished.
-
-The above creates a single table with three columns: id, title, and text. The 'id' field is the primary key and will
+The above creates a single table with three columns: I<id>, I<title>, and
+I<text>. The 'I<id>' field is the primary key and will
automatically get an ID assigned by the database engine when a row is inserted.
@@ -207,7 +205,10 @@ C</add> route handler which handles a POST operation.
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute(params->{'title'}, params->{'text'}) or die $sth->errstr;
- set_flash('New entry posted!');
+ # note: 'flash' keyword imported by Dancer::Plugin::FlashMessage,
+ # not part of Dancer core
+ flash message => 'New entry posted!';
+
redirect '/';
};
@@ -227,23 +228,6 @@ method to pull in the parameters in the current HTTP request. (You can see the
Those values are inserted into the database, then we set a flash message for
the user and redirect her back to the root URL.
-It's worth mentioning that the "flash message" is not part of Dancer, but a
-part of this specific application.
-
-Here is the code in case you need it:
-
- use Dancer::Plugin::FlashMessage;
-
- sub set_flash {
- my $message = shift;
- flash warning => $message;
- }
-
- sub get_flash {
- my $message = flash 'warning';
- return $message;
- }
-
=head1 Logins and sessions
Dancer comes with a simple in-memory session manager out of the box. It

0 comments on commit dd06ba2

Please sign in to comment.