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