fix tiny typo in PLP::HowTo title
[perl/plp/.git] / lib / PLP / HowTo.pod
1 =head1 NAME
2
3 PLP::HowTo - Some examples of common web things in PLP.
4
5 =head1 HOW TO...
6
7 Additional Perl functionality is often available in modules. All of the modules
8 used in this document are available (for free) at CPAN: http://search.cpan.org/
9
10 =head2 send a cookie
11
12  <: 
13      BEGIN {
14          use CGI::Cookie;
15          AddCookie(
16              CGI::Cookie->new(
17                  -name => 'ID',
18                  -value => 123456,
19                  -domain => 'foo.com',
20                  -path => '/'
21              )->as_string
22          );
23      }
24  :>
25
26 =head2 get a cookie
27
28  Your user ID is <:= $cookie{ID} :>
29
30 =head2 set a header
31
32  <:
33      BEGIN {
34          $header{Content_Type} = 'text/plain';
35      }
36  :>
37
38 =head2 use a database
39
40 Use DBI, and alternatively, one of the many simplifying modules. Drivers for
41 DBI are in the DBD:: namespace. DBI loads the driver automatically, but it has
42 to be available. If you need a fast full-featured file-base database, use
43 DBD::SQLite, it's the instant database :).
44
45  <:
46      use DBIx::Simple; # and read its documentation for examples.
47  :>
48
49 =head2 allow a user to upload a file
50
51 Use CGI.pm, which can be used with CGI::Upload to make things easier
52
53  <:
54      use CGI;         # and don't use %post in your PLP document.
55      use CGI::Upload; # and read its documentation for examples.
56      my $cgi = CGI->new;
57      my $upload = CGI::Upload->new($cgi);
58      ...
59  :>
60
61 =head2 download a file into a variable
62
63  <:
64      use LWP::Simple;
65      my $page = get 'http://foo.com/bar.html';
66  :>
67
68 =head2 implement basic authentication
69
70 This only works with PLP under mod_perl. For CGI installations, it's useless.
71
72  <:
73      use MIME::Base64;
74
75      BEGIN {
76          my $r = Apache->request;
77
78          my ($type, $login) = split / /, $r->header_in('Authorization');
79          my ($user, $pass) = split /:/, decode_base64 $login, 2;
80
81          unless ($user eq 'foo' and $pass eq 'bar') {
82              $header{Status} = '401 Authorization Required';
83              $header{WWW_Authenticate} = 'Basic realm="Top secret :)"';
84              print '<h1>Authorization Required</h1>';
85              exit;
86          }
87      }
88  :>
89
90 (It is possible to use something similar with CGI, but it's not easy. Headers
91 are communicated to your script via C<%ENV>, and having credentials in there
92 would be insecure, so Apache removes them. To get C<$ENV{HTTP_AUTHORIZATION}>,
93 you need to recompile Apache with -DSECURITY_HOLE_PASS_AUTHORIZATION, or use
94 mod_rewrite to set the environment variable. Short answer: just use mod_perl.)
95
96 =head1 FEEDBACK
97
98 If you have good, simple examples of how to do common things with PLP, please
99 send them! <perl@shiar.org>
100
101 =cut
102