3 Catalyst::Manual::Tutorial::Appendices - Catalyst Tutorial - Part 9: Appendices
8 This is B<Part 9 of 9> of the Catalyst tutorial.
10 L<Tutorial Overview|Catalyst::Manual::Tutorial>
16 L<Introduction|Catalyst::Manual::Tutorial::Intro>
20 L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
24 L<Basic CRUD|Catalyst::Manual::Tutorial_BasicCRUD>
28 L<Authentication|Catalyst::Manual::Tutorial::Authentication>
32 L<Authorization|Catalyst::Manual::Tutorial::Authorization>
36 L<Debugging|Catalyst::Manual::Tutorial::Debugging>
40 L<Testing|Catalyst::Manual::Tutorial::Testing>
44 L<AdvancedCRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
55 This part of the tutorial provides supporting information relevant to
56 the Catalyst tutorial.
59 =head1 APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES
61 You may notice that Pod indents example code with four spaces. This
62 section provides some quick advice to "un-indent" this text in common
65 =head2 "Un-indenting" with Vi/Vim
67 When cutting and pasting multi-line text from Pod-based documents, the
68 following vi/vim regexs can be helpful to "un-indent" the inserted text
69 (do NOT type the quotes, they are only included to show spaces in the
70 regex patterns). I<Note that all 3 of the regexs end in 4 spaces>:
78 Removes four leading spaces from the entire file (from the first line,
79 C<0>, to the last line, C<$>).
85 A shortcut for the previous item (C<%> specifies the entire file; so
86 this removes four leading spaces from every line).
92 Removes the first four spaces from the line the cursor is on at the time
93 the regex command is executed (".") to the last line of the file.
99 Removes four leading space from the current line through line 44
100 (obviously adjust the C<44> to the appropriate value in your example).
104 =head2 "Un-indenting" with Emacs
106 Although there author has not used emacs for many years (apologies to
107 the emacs fans out there), here is a quick hint to get you started. To
108 replace the leading spaces of every line in a file, use:
110 M-x replace-regexp<RET>
111 Replace regexp: ^ <RET>
114 All of that will occur on the single line at the bottom of your screen.
115 Note that "<RET>" represents the return key/enter. Also, there are
116 four spaces after the "^" on the "Replace regexp:" line and no spaces
117 entered on the last line.
119 You can limit the replacement operation by selecting text first (depending
120 on your version of emacs, you can either use the mouse or experiment with
121 commands such as C<C-SPC> to set the mark at the cursor location and
122 C<C-E<lt>> and C<C-E<gt>> to set the mark at the beginning and end of the
126 =head1 APPENDIX 2: USING MYSQL AND POSTGRESQL
128 The main database used in this tutorial is the very simple yet powerful
129 SQLite. This section provides information that can be used to "convert"
130 the tutorial to use MySQL and PostgreSQL. However, note that part of
131 the beauty of the MVC architecture is that very little database-specific
132 code is spread throughout the system (at least when MVC is "done
133 right"). Consequently, converting from one database to another is
134 relatively painless with most Catalyst applications. In general, you
135 just need to adapt the schema definition C<.sql> file you use to
136 initialize your database and adjust a few configuration parameters.
138 Also note that the purpose of the data definition statements for this
139 section are not designed to take maximum advantage of the various
140 features in each database for issues such as referential integrity and
141 field types/constraints.
145 Use the following steps to adapt the tutorial to MySQL. Thanks to Jim
152 Part 2: Catalyst Basics
158 Install the required software:
164 The MySQL database server and client utility.
168 The Perl C<DBD::MySQL> module
172 For CentOS users (see
173 L<Catalyst::Manual::Installation::CentOS4|Catalyst::Manual::Installation::CentOS4>),
174 you can use the following commands to install the software and start the MySQL
177 yum -y install mysql mysql-server
182 Create the database and set the permissions:
185 Welcome to the MySQL monitor. Commands end with ; or \g.
186 Your MySQL connection id is 2 to server version: 4.1.20
188 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
190 mysql> create database myapp;
191 Query OK, 1 row affected (0.01 sec)
193 mysql> grant all on myapp.* to tutorial@'localhost';
194 Query OK, 0 rows affected (0.00 sec)
196 mysql> flush privileges;
197 Query OK, 0 rows affected (0.00 sec)
204 Create the C<.sql> file and load the data:
210 Open the C<myapp01_mysql.sql> in your editor and enter:
213 -- Create a very simple database to hold book and author information
215 DROP TABLE IF EXISTS books;
216 DROP TABLE IF EXISTS book_authors;
217 DROP TABLE IF EXISTS authors;
219 id INT(11) PRIMARY KEY AUTO_INCREMENT,
223 -- 'book_authors' is a many-to-many join table between books & authors
224 CREATE TABLE book_authors (
227 PRIMARY KEY (book_id, author_id)
229 CREATE TABLE authors (
230 id INT(11) PRIMARY KEY AUTO_INCREMENT,
235 --- Load some sample data
237 INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
238 INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
239 INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
240 INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
241 INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
242 INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
243 INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
244 INSERT INTO authors VALUES (3, 'Christian', 'Degu');
245 INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
246 INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
247 INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
248 INSERT INTO authors VALUES (7, ' Nathan', 'Torkington');
249 INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
250 INSERT INTO book_authors VALUES (1, 1);
251 INSERT INTO book_authors VALUES (1, 2);
252 INSERT INTO book_authors VALUES (1, 3);
253 INSERT INTO book_authors VALUES (2, 4);
254 INSERT INTO book_authors VALUES (3, 5);
255 INSERT INTO book_authors VALUES (4, 6);
256 INSERT INTO book_authors VALUES (4, 7);
257 INSERT INTO book_authors VALUES (5, 8);
263 mysql -ututorial myapp < myapp01_mysql.sql
267 Make sure the data loaded correctly:
269 $ mysql -ututorial myapp
270 Reading table information for completion of table and column names
271 You can turn off this feature to get a quicker startup with -A
273 Welcome to the MySQL monitor. Commands end with ; or \g.
274 Your MySQL connection id is 4 to server version: 4.1.20
276 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
286 3 rows in set (0.00 sec)
288 mysql> select * from books;
289 +----+------------------------------------+--------+
290 | id | title | rating |
291 +----+------------------------------------+--------+
292 | 1 | CCSP SNRS Exam Certification Guide | 5 |
293 | 2 | TCP/IP Illustrated, Volume 1 | 5 |
294 | 3 | Internetworking with TCP/IP Vol.1 | 4 |
295 | 4 | Perl Cookbook | 5 |
296 | 5 | Designing with Web Standards | 5 |
297 +----+------------------------------------+--------+
298 5 rows in set (0.00 sec)
312 Delete the existing model:
314 rm lib/MyApp/Model/MyAppDB.pm
318 Regenerate the model using the Catalyst "_create.pl" script:
320 script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB dbi:mysql:myapp 'tutorial' '' '{ AutoCommit => 1 }'
328 Part 4: Authentication
334 Create the C<.sql> file for the user/roles data:
336 Open C<myapp02_mysql.sql> in your editor and enter:
339 -- Add users and roles tables, along with a many-to-many join table
342 id INT(11) PRIMARY KEY,
351 id INTEGER PRIMARY KEY,
354 CREATE TABLE user_roles (
357 PRIMARY KEY (user_id, role_id)
360 -- Load up some initial test data
362 INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
363 INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
364 INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
365 INSERT INTO roles VALUES (1, 'user');
366 INSERT INTO roles VALUES (2, 'admin');
367 INSERT INTO user_roles VALUES (1, 1);
368 INSERT INTO user_roles VALUES (1, 2);
369 INSERT INTO user_roles VALUES (2, 1);
370 INSERT INTO user_roles VALUES (3, 1);
374 Load the user/roles data:
376 mysql -ututorial myapp < myapp02_mysql.sql
380 Create the C<.sql> file for the hashed password data:
382 Open C<myapp03_mysql.sql> in your editor and enter:
385 -- Convert passwords to SHA-1 hashes
387 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
388 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
389 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
393 Load the user/roles data:
395 mysql -ututorial myapp < myapp03_mysql.sql
403 B<TODO> -- Please see the latest version of this document for possible updates:
404 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Runtime/lib/Catalyst/Manual/Tutorial/Appendices.pod>
407 =head1 APPENDIX 3: IMPROVED HASHING SCRIPT
409 Here is an improved SHA-1 hashing script from Gavin Henry that does
410 not expose the passwords to "capture" on the command line.
413 #===============================================================================
417 # USAGE: ./enc_pass.pl
419 # DESCRIPTION: Encrypt a Password using SHA-1
425 # AUTHOR: Gavin Henry (GH), <ghenry@suretecsystems.com>
426 # COMPANY: Suretec Systems Ltd.
428 # CREATED: 26/06/2006
430 # COPYRIGHT: http://search.cpan.org/dist/perl/pod/perlgpl.pod
431 #===============================================================================
440 chomp( my $pw = ReadLine 0 );
445 print "Enter the password to be encrypted: ";
446 my $pass = get_pass();
448 print "\nConfirm the password: ";
449 my $verify = get_pass();
451 if ( $pass eq $verify ) {
452 my $sha1_enc = Digest::SHA1->new;
453 $sha1_enc->add($pass);
455 print "\nYour encrypted password is: "
456 . $sha1_enc->hexdigest . "\n"
457 . "Paste this into your SQL INSERT/COPY Data.\n";
460 print "\nPasswords do not match!\n";
466 Kennedy Clark, C<hkclark@gmail.com>
468 Please report any errors, issues or suggestions to the author. The
469 most recent version of the Catalyst Tutorial can be found at
470 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Runtime/lib/Catalyst/Manual/Tutorial/>.
472 Copyright 2006, Kennedy Clark, under Creative Commons License
473 (L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).