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>
54 This part of the tutorial provides supporting information relevant to
55 the Catalyst tutorial.
57 =head1 APPENDIX 1: CUT AND PASTE FOR POD-BASED EXAMPLES
59 You may notice that Pod indents example code with four spaces. This
60 section provides some quick advice to "un-indent" this text in common
63 =head2 "Un-indenting" with Vi/Vim
65 When cutting and pasting multi-line text from Pod-based documents, the
66 following vi/vim regexs can be helpful to "un-indent" the inserted text
67 (do NOT type the quotes, they are only included to show spaces in the
68 regex patterns). I<Note that all 3 of the regexs end in 4 spaces>:
76 Removes four leading spaces from the entire file (from the first line,
77 C<0>, to the last line, C<$>).
83 A shortcut for the previous item (C<%> specifies the entire file; so
84 this removes four leading spaces from every line).
90 Removes the first four spaces from the line the cursor is on at the time
91 the regex command is executed (".") to the last line of the file.
97 Removes four leading space from the current line through line 44
98 (obviously adjust the C<44> to the appropriate value in your example).
102 =head2 "Un-indenting" with Emacs
106 =head1 APPENDIX 2: USING MYSQL AND POSTGRESQL
108 The main database used in this tutorial is the very simple yet powerful
109 SQLite. This section provides information that can be used to "convert"
110 the tutorial to use MySQL and PostgreSQL. However, note that part of
111 the beauty of the MVC architecture is that very little database-specific
112 code is spread throughout the system (at least when MVC is "done
113 right"). Consequently, converting from one database to another is
114 relatively painless with most Catalyst applications. In general, you
115 just need to adapt the schema definition C<.sql> file you use to
116 initialize your database and adjust a few configuration parameters.
118 Also note that the purpose of the data definition statements for this
119 section are not designed to take maximum advantage of the various
120 features in each database for issues such as referential integrity and
121 field types/constraints.
125 Use the following steps to adapt the tutorial to MySQL. Thanks to Jim
132 Part 2: Catalyst Basics
138 Install the required software:
144 The MySQL database server and client utility.
148 The Perl C<DBD::MySQL> module
152 For CentOS users (see
153 L<Catalyst::Manual::Installation::CentOS4|Catalyst::Manual::Installation::CentOS4>),
154 you can use the following commands to install the software and start the MySQL
157 yum -y install mysql mysql-server
162 Create the database and set the permissions:
165 Welcome to the MySQL monitor. Commands end with ; or \g.
166 Your MySQL connection id is 2 to server version: 4.1.20
168 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
170 mysql> create database myapp;
171 Query OK, 1 row affected (0.01 sec)
173 mysql> grant all on myapp.* to tutorial@'localhost';
174 Query OK, 0 rows affected (0.00 sec)
176 mysql> flush privileges;
177 Query OK, 0 rows affected (0.00 sec)
184 Create the C<.sql> file and load the data:
190 Open the C<myapp01_mysql.sql> in your editor and enter:
193 -- Create a very simple database to hold book and author information
195 DROP TABLE IF EXISTS books;
196 DROP TABLE IF EXISTS book_authors;
197 DROP TABLE IF EXISTS authors;
199 id INT(11) PRIMARY KEY,
203 -- 'book_authors' is a many-to-many join table between books & authors
204 CREATE TABLE book_authors (
207 PRIMARY KEY (book_id, author_id)
209 CREATE TABLE authors (
210 id INT(11) PRIMARY KEY,
215 --- Load some sample data
217 INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
218 INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
219 INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
220 INSERT INTO books VALUES (4, 'Perl Cookbook', 5);
221 INSERT INTO books VALUES (5, 'Designing with Web Standards', 5);
222 INSERT INTO authors VALUES (1, 'Greg', 'Bastien');
223 INSERT INTO authors VALUES (2, 'Sara', 'Nasseh');
224 INSERT INTO authors VALUES (3, 'Christian', 'Degu');
225 INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
226 INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
227 INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
228 INSERT INTO authors VALUES (7, ' Nathan', 'Torkington');
229 INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
230 INSERT INTO book_authors VALUES (1, 1);
231 INSERT INTO book_authors VALUES (1, 2);
232 INSERT INTO book_authors VALUES (1, 3);
233 INSERT INTO book_authors VALUES (2, 4);
234 INSERT INTO book_authors VALUES (3, 5);
235 INSERT INTO book_authors VALUES (4, 6);
236 INSERT INTO book_authors VALUES (4, 7);
237 INSERT INTO book_authors VALUES (5, 8);
243 mysql -ututorial myapp < myapp01_mysql.sql
247 Make sure the data loaded correctly:
249 $ mysql -ututorial myapp
250 Reading table information for completion of table and column names
251 You can turn off this feature to get a quicker startup with -A
253 Welcome to the MySQL monitor. Commands end with ; or \g.
254 Your MySQL connection id is 4 to server version: 4.1.20
256 Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
266 3 rows in set (0.00 sec)
268 mysql> select * from books;
269 +----+------------------------------------+--------+
270 | id | title | rating |
271 +----+------------------------------------+--------+
272 | 1 | CCSP SNRS Exam Certification Guide | 5 |
273 | 2 | TCP/IP Illustrated, Volume 1 | 5 |
274 | 3 | Internetworking with TCP/IP Vol.1 | 4 |
275 | 4 | Perl Cookbook | 5 |
276 | 5 | Designing with Web Standards | 5 |
277 +----+------------------------------------+--------+
278 5 rows in set (0.00 sec)
292 Delete the existing model:
294 rm lib/MyApp/Model/MyAppDB.pm
298 Regenerate the model using the Catalyst "_create.pl" script:
300 script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB dbi:mysql:myapp 'tutorial' '' '{ AutoCommit => 1 }'
308 Part 4: Authentication
314 Create the C<.sql> file for the user/roles data:
316 Open C<myapp02_mysql.sql> in your editor and enter:
319 -- Add users and roles tables, along with a many-to-many join table
322 id INT(11) PRIMARY KEY,
331 id INTEGER PRIMARY KEY,
334 CREATE TABLE user_roles (
337 PRIMARY KEY (user_id, role_id)
340 -- Load up some initial test data
342 INSERT INTO users VALUES (1, 'test01', 'mypass', 't01@na.com', 'Joe', 'Blow', 1);
343 INSERT INTO users VALUES (2, 'test02', 'mypass', 't02@na.com', 'Jane', 'Doe', 1);
344 INSERT INTO users VALUES (3, 'test03', 'mypass', 't03@na.com', 'No', 'Go', 0);
345 INSERT INTO roles VALUES (1, 'user');
346 INSERT INTO roles VALUES (2, 'admin');
347 INSERT INTO user_roles VALUES (1, 1);
348 INSERT INTO user_roles VALUES (1, 2);
349 INSERT INTO user_roles VALUES (2, 1);
350 INSERT INTO user_roles VALUES (3, 1);
354 Load the user/roles data:
356 mysql -ututorial myapp < myapp02_mysql.sql
360 Create the C<.sql> file for the hashed password data:
362 Open C<myapp03_mysql.sql> in your editor and enter:
365 -- Convert passwords to SHA-1 hashes
367 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 1;
368 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 2;
369 UPDATE users SET password = 'e727d1464ae12436e899a726da5b2f11d8381b26' WHERE id = 3;
373 Load the user/roles data:
375 mysql -ututorial myapp < myapp03_mysql.sql
386 =head1 APPENDIX 3: IMPROVED HASHING SCRIPT
388 Here is an improved SHA-1 hashing script from Gavin Henry that does
389 not expose the passwords to "capture" on the command line.
392 #===============================================================================
396 # USAGE: ./enc_pass.pl
398 # DESCRIPTION: Encrypt a Password using SHA-1
404 # AUTHOR: Gavin Henry (GH), <ghenry@suretecsystems.com>
405 # COMPANY: Suretec Systems Ltd.
407 # CREATED: 26/06/2006
409 # COPYRIGHT: http://search.cpan.org/dist/perl/pod/perlgpl.pod
410 #===============================================================================
419 chomp( my $pw = ReadLine 0 );
424 print "Enter the password to be encrypted: ";
425 my $pass = get_pass();
427 print "\nConfirm the password: ";
428 my $verify = get_pass();
430 if ( $pass eq $verify ) {
431 my $sha1_enc = Digest::SHA1->new;
432 $sha1_enc->add($pass);
434 print "\nYour encrypted password is: "
435 . $sha1_enc->hexdigest . "\n"
436 . "Paste this into your SQL INSERT/COPY Data.\n";
439 print "\nPasswords do not match!\n";
446 Kennedy Clark, C<hkclark@gmail.com>
448 Please report any errors, issues or suggestions to the author. The
449 most recent version of the Catlayst Tutorial can be found at
450 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/Catalyst-Runtime/lib/Catalyst/Manual/Tutorial/>.
452 Copyright 2006, Kennedy Clark, under Creative Commons License
453 (L<http://creativecommons.org/licenses/by-nc-sa/2.5/>).