1perlfaq9(3)           User Contributed Perl Documentation          perlfaq9(3)
2
3
4

NAME

6       perlfaq9 - Web, Email and Networking
7

VERSION

9       version 5.20180605
10

DESCRIPTION

12       This section deals with questions related to running web sites, sending
13       and receiving email as well as general networking.
14
15   Should I use a web framework?
16       Yes. If you are building a web site with any level of interactivity
17       (forms / users / databases), you will want to use a framework to make
18       handling requests and responses easier.
19
20       If there is no interactivity then you may still want to look at using
21       something like Template Toolkit <https://metacpan.org/module/Template>
22       or Plack::Middleware::TemplateToolkit so maintenance of your HTML files
23       (and other assets) is easier.
24
25   Which web framework should I use?
26       There is no simple answer to this question. Perl frameworks can run
27       everything from basic file servers and small scale intranets to massive
28       multinational multilingual websites that are the core to international
29       businesses.
30
31       Below is a list of a few frameworks with comments which might help you
32       in making a decision, depending on your specific requirements. Start by
33       reading the docs, then ask questions on the relevant mailing list or
34       IRC channel.
35
36       Catalyst
37           Strongly object-oriented and fully-featured with a long development
38           history and a large community and addon ecosystem. It is excellent
39           for large and complex applications, where you have full control
40           over the server.
41
42       Dancer
43           Young and free of legacy weight, providing a lightweight and easy
44           to learn API.  Has a growing addon ecosystem. It is best used for
45           smaller projects and very easy to learn for beginners.
46
47       Mojolicious
48           Fairly young with a focus on HTML5 and real-time web technologies
49           such as WebSockets.
50
51       Web::Simple
52           Currently experimental, strongly object-oriented, built for speed
53           and intended as a toolkit for building micro web apps, custom
54           frameworks or for tieing together existing Plack-compatible web
55           applications with one central dispatcher.
56
57       All of these interact with or use Plack which is worth understanding
58       the basics of when building a website in Perl (there is a lot of useful
59       Plack::Middleware
60       <https://metacpan.org/search?q=plack%3A%3Amiddleware>).
61
62   What is Plack and PSGI?
63       PSGI is the Perl Web Server Gateway Interface Specification, it is a
64       standard that many Perl web frameworks use, you should not need to
65       understand it to build a web site, the part you might want to use is
66       Plack.
67
68       Plack is a set of tools for using the PSGI stack. It contains
69       middleware <https://metacpan.org/search?q=plack%3A%3Amiddleware>
70       components, a reference server and utilities for Web application
71       frameworks.  Plack is like Ruby's Rack or Python's Paste for WSGI.
72
73       You could build a web site using Plack and your own code, but for
74       anything other than a very basic web site, using a web framework (that
75       uses Plack) is a better option.
76
77   How do I remove HTML from a string?
78       Use HTML::Strip, or HTML::FormatText which not only removes HTML but
79       also attempts to do a little simple formatting of the resulting plain
80       text.
81
82   How do I extract URLs?
83       HTML::SimpleLinkExtor will extract URLs from HTML, it handles anchors,
84       images, objects, frames, and many other tags that can contain a URL.
85       If you need anything more complex, you can create your own subclass of
86       HTML::LinkExtor or HTML::Parser. You might even use
87       HTML::SimpleLinkExtor as an example for something specifically suited
88       to your needs.
89
90       You can use URI::Find to extract URLs from an arbitrary text document.
91
92   How do I fetch an HTML file?
93       (contributed by brian d foy)
94
95       Use the libwww-perl distribution. The LWP::Simple module can fetch web
96       resources and give their content back to you as a string:
97
98           use LWP::Simple qw(get);
99
100           my $html = get( "http://www.example.com/index.html" );
101
102       It can also store the resource directly in a file:
103
104           use LWP::Simple qw(getstore);
105
106           getstore( "http://www.example.com/index.html", "foo.html" );
107
108       If you need to do something more complicated, you can use
109       LWP::UserAgent module to create your own user-agent (e.g. browser) to
110       get the job done. If you want to simulate an interactive web browser,
111       you can use the WWW::Mechanize module.
112
113   How do I automate an HTML form submission?
114       If you are doing something complex, such as moving through many pages
115       and forms or a web site, you can use WWW::Mechanize. See its
116       documentation for all the details.
117
118       If you're submitting values using the GET method, create a URL and
119       encode the form using the "query_form" method:
120
121           use LWP::Simple;
122           use URI::URL;
123
124           my $url = url('L<http://www.perl.com/cgi-bin/cpan_mod')>;
125           $url->query_form(module => 'DB_File', readme => 1);
126           $content = get($url);
127
128       If you're using the POST method, create your own user agent and encode
129       the content appropriately.
130
131           use HTTP::Request::Common qw(POST);
132           use LWP::UserAgent;
133
134           my $ua = LWP::UserAgent->new();
135           my $req = POST 'L<http://www.perl.com/cgi-bin/cpan_mod'>,
136                          [ module => 'DB_File', readme => 1 ];
137           my $content = $ua->request($req)->as_string;
138
139   How do I decode or create those %-encodings on the web?
140       Most of the time you should not need to do this as your web framework,
141       or if you are making a request, the LWP or other module would handle it
142       for you.
143
144       To encode a string yourself, use the URI::Escape module. The
145       "uri_escape" function returns the escaped string:
146
147           my $original = "Colon : Hash # Percent %";
148
149           my $escaped = uri_escape( $original );
150
151           print "$escaped\n"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25'
152
153       To decode the string, use the "uri_unescape" function:
154
155           my $unescaped = uri_unescape( $escaped );
156
157           print $unescaped; # back to original
158
159       Remember not to encode a full URI, you need to escape each component
160       separately and then join them together.
161
162   How do I redirect to another page?
163       Most Perl Web Frameworks will have a mechanism for doing this, using
164       the Catalyst framework it would be:
165
166           $c->res->redirect($url);
167           $c->detach();
168
169       If you are using Plack (which most frameworks do), then
170       Plack::Middleware::Rewrite is worth looking at if you are migrating
171       from Apache or have URL's you want to always redirect.
172
173   How do I put a password on my web pages?
174       See if the web framework you are using has an authentication system and
175       if that fits your needs.
176
177       Alternativly look at Plack::Middleware::Auth::Basic, or one of the
178       other Plack authentication <https://metacpan.org/search?q=plack+auth>
179       options.
180
181   How do I make sure users can't enter values into a form that causes my CGI
182       script to do bad things?
183       (contributed by brian d foy)
184
185       You can't prevent people from sending your script bad data. Even if you
186       add some client-side checks, people may disable them or bypass them
187       completely. For instance, someone might use a module such as LWP to
188       submit to your web site. If you want to prevent data that try to use
189       SQL injection or other sorts of attacks (and you should want to), you
190       have to not trust any data that enter your program.
191
192       The perlsec documentation has general advice about data security.  If
193       you are using the DBI module, use placeholder to fill in data.  If you
194       are running external programs with "system" or "exec", use the list
195       forms. There are many other precautions that you should take, too many
196       to list here, and most of them fall under the category of not using any
197       data that you don't intend to use. Trust no one.
198
199   How do I parse a mail header?
200       Use the Email::MIME module. It's well-tested and supports all the
201       craziness that you'll see in the real world (comment-folding
202       whitespace, encodings, comments, etc.).
203
204         use Email::MIME;
205
206         my $message = Email::MIME->new($rfc2822);
207         my $subject = $message->header('Subject');
208         my $from    = $message->header('From');
209
210       If you've already got some other kind of email object, consider passing
211       it to Email::Abstract and then using its cast method to get an
212       Email::MIME object:
213
214         my $abstract = Email::Abstract->new($mail_message_object);
215         my $email_mime_object = $abstract->cast('Email::MIME');
216
217   How do I check a valid mail address?
218       (partly contributed by Aaron Sherman)
219
220       This isn't as simple a question as it sounds. There are two parts:
221
222       a) How do I verify that an email address is correctly formatted?
223
224       b) How do I verify that an email address targets a valid recipient?
225
226       Without sending mail to the address and seeing whether there's a human
227       on the other end to answer you, you cannot fully answer part b, but the
228       Email::Valid module will do both part a and part b as far as you can in
229       real-time.
230
231       Our best advice for verifying a person's mail address is to have them
232       enter their address twice, just as you normally do to change a
233       password. This usually weeds out typos. If both versions match, send
234       mail to that address with a personal message. If you get the message
235       back and they've followed your directions, you can be reasonably
236       assured that it's real.
237
238       A related strategy that's less open to forgery is to give them a PIN
239       (personal ID number). Record the address and PIN (best that it be a
240       random one) for later processing. In the mail you send, include a link
241       to your site with the PIN included. If the mail bounces, you know it's
242       not valid. If they don't click on the link, either they forged the
243       address or (assuming they got the message) following through wasn't
244       important so you don't need to worry about it.
245
246   How do I decode a MIME/BASE64 string?
247       The MIME::Base64 package handles this as well as the MIME/QP encoding.
248       Decoding base 64 becomes as simple as:
249
250           use MIME::Base64;
251           my $decoded = decode_base64($encoded);
252
253       The Email::MIME module can decode base 64-encoded email message parts
254       transparently so the developer doesn't need to worry about it.
255
256   How do I find the user's mail address?
257       Ask them for it. There are so many email providers available that it's
258       unlikely the local system has any idea how to determine a user's email
259       address.
260
261       The exception is for organization-specific email (e.g.
262       foo@yourcompany.com) where policy can be codified in your program. In
263       that case, you could look at $ENV{USER}, $ENV{LOGNAME}, and
264       getpwuid($<) in scalar context, like so:
265
266         my $user_name = getpwuid($<)
267
268       But you still cannot make assumptions about whether this is correct,
269       unless your policy says it is. You really are best off asking the user.
270
271   How do I send email?
272       Use the Email::MIME and Email::Sender::Simple modules, like so:
273
274         # first, create your message
275         my $message = Email::MIME->create(
276           header_str => [
277             From    => 'you@example.com',
278             To      => 'friend@example.com',
279             Subject => 'Happy birthday!',
280           ],
281           attributes => {
282             encoding => 'quoted-printable',
283             charset  => 'utf-8',
284           },
285           body_str => "Happy birthday to you!\n",
286         );
287
288         use Email::Sender::Simple qw(sendmail);
289         sendmail($message);
290
291       By default, Email::Sender::Simple will try `sendmail` first, if it
292       exists in your $PATH. This generally isn't the case. If there's a
293       remote mail server you use to send mail, consider investigating one of
294       the Transport classes. At time of writing, the available transports
295       include:
296
297       Email::Sender::Transport::Sendmail
298           This is the default. If you can use the mail(1) or mailx(1) program
299           to send mail from the machine where your code runs, you should be
300           able to use this.
301
302       Email::Sender::Transport::SMTP
303           This transport contacts a remote SMTP server over TCP. It
304           optionally uses TLS or SSL and can authenticate to the server via
305           SASL.
306
307       Telling Email::Sender::Simple to use your transport is straightforward.
308
309         sendmail(
310           $message,
311           {
312             transport => $email_sender_transport_object,
313           }
314         );
315
316   How do I use MIME to make an attachment to a mail message?
317       Email::MIME directly supports multipart messages. Email::MIME objects
318       themselves are parts and can be attached to other Email::MIME objects.
319       Consult the Email::MIME documentation for more information, including
320       all of the supported methods and examples of their use.
321
322   How do I read email?
323       Use the Email::Folder module, like so:
324
325         use Email::Folder;
326
327         my $folder = Email::Folder->new('/path/to/email/folder');
328         while(my $message = $folder->next_message) {
329           # next_message returns Email::Simple objects, but we want
330           # Email::MIME objects as they're more robust
331           my $mime = Email::MIME->new($message->as_string);
332         }
333
334       There are different classes in the Email::Folder namespace for
335       supporting various mailbox types. Note that these modules are generally
336       rather limited and only support reading rather than writing.
337
338   How do I find out my hostname, domainname, or IP address?
339       (contributed by brian d foy)
340
341       The Net::Domain module, which is part of the Standard Library starting
342       in Perl 5.7.3, can get you the fully qualified domain name (FQDN), the
343       host name, or the domain name.
344
345           use Net::Domain qw(hostname hostfqdn hostdomain);
346
347           my $host = hostfqdn();
348
349       The Sys::Hostname module, part of the Standard Library, can also get
350       the hostname:
351
352           use Sys::Hostname;
353
354           $host = hostname();
355
356       The Sys::Hostname::Long module takes a different approach and tries
357       harder to return the fully qualified hostname:
358
359         use Sys::Hostname::Long 'hostname_long';
360
361         my $hostname = hostname_long();
362
363       To get the IP address, you can use the "gethostbyname" built-in
364       function to turn the name into a number. To turn that number into the
365       dotted octet form (a.b.c.d) that most people expect, use the
366       "inet_ntoa" function from the Socket module, which also comes with
367       perl.
368
369           use Socket;
370
371           my $address = inet_ntoa(
372               scalar gethostbyname( $host || 'localhost' )
373           );
374
375   How do I fetch/put an (S)FTP file?
376       Net::FTP, and Net::SFTP allow you to interact with FTP and SFTP (Secure
377       FTP) servers.
378
379   How can I do RPC in Perl?
380       Use one of the RPC modules( <https://metacpan.org/search?q=RPC> ).
381
383       Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and other
384       authors as noted. All rights reserved.
385
386       This documentation is free; you can redistribute it and/or modify it
387       under the same terms as Perl itself.
388
389       Irrespective of its distribution, all code examples in this file are
390       hereby placed into the public domain. You are permitted and encouraged
391       to use this code in your own programs for fun or for profit as you see
392       fit. A simple comment in the code giving credit would be courteous but
393       is not required.
394
395
396
397perl v5.26.3                      2018-06-05                       perlfaq9(3)
Impressum