Debian Codegraph @

upstream/debian-unstable/man1/perlfaq9.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "PERLFAQ9 1"
58 .TH PERLFAQ9 1 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 perlfaq9 \- Web, Email and Networking
65 .SH VERSION
66 .IX Header "VERSION"
67 version 5.20240218
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 This section deals with questions related to running web sites,
71 sending and receiving email as well as general networking.
72 .SS "Should I use a web framework?"
73 .IX Subsection "Should I use a web framework?"
74 Yes. If you are building a web site with any level of interactivity
75 (forms / users / databases), you
76 will want to use a framework to make handling requests
77 and responses easier.
78 .PP
79 If there is no interactivity then you may still want
80 to look at using something like Template Toolkit <https://metacpan.org/module/Template>
81 or Plack::Middleware::TemplateToolkit
82 so maintenance of your HTML files (and other assets) is easier.
83 .SS "Which web framework should I use?"
84 .IX Xref "framework CGI.pm CGI Catalyst Dancer"
85 .IX Subsection "Which web framework should I use?"
86 There is no simple answer to this question. Perl frameworks can run everything
87 from basic file servers and small scale intranets to massive multinational
88 multilingual websites that are the core to international businesses.
89 .PP
90 Below is a list of a few frameworks with comments which might help you in
91 making a decision, depending on your specific requirements. Start by reading
92 the docs, then ask questions on the relevant mailing list or IRC channel.
93 .IP Catalyst 4
94 .IX Item "Catalyst"
95 Strongly object-oriented and fully-featured with a long development history and
96 a large community and addon ecosystem. It is excellent for large and complex
97 applications, where you have full control over the server.
98 .IP Dancer2 4
99 .IX Item "Dancer2"
100 Free of legacy weight, providing a lightweight and easy to learn API.
101 Has a growing addon ecosystem. It is best used for smaller projects and
102 very easy to learn for beginners.
103 .IP Mojolicious 4
104 .IX Item "Mojolicious"
105 Self-contained and powerful for both small and larger projects,
106 with a focus on HTML5 and real-time web technologies such as WebSockets.
107 .IP Web::Simple 4
108 .IX Item "Web::Simple"
109 Strongly object-oriented and minimal, built for speed and intended
110 as a toolkit for building micro web apps, custom frameworks or for tieing
111 together existing Plack-compatible web applications with one central dispatcher.
112 .PP
113 All of these interact with or use Plack which is worth understanding
114 the basics of when building a website in Perl (there is a lot of useful
115 Plack::Middleware <https://metacpan.org/search?q=plack%3A%3Amiddleware>).
116 .SS "What is Plack and PSGI?"
117 .IX Subsection "What is Plack and PSGI?"
118 PSGI is the Perl Web Server Gateway Interface Specification, it is
119 a standard that many Perl web frameworks use, you should not need to
120 understand it to build a web site, the part you might want to use is Plack.
121 .PP
122 Plack is a set of tools for using the PSGI stack. It contains
123 middleware <https://metacpan.org/search?q=plack%3A%3Amiddleware>
124 components, a reference server and utilities for Web application frameworks.
125 Plack is like Ruby's Rack or Python's Paste for WSGI.
126 .PP
127 You could build a web site using Plack and your own code,
128 but for anything other than a very basic web site, using a web framework
129 (that uses <https://plackperl.org>) is a better option.
130 .SS "How do I remove HTML from a string?"
131 .IX Subsection "How do I remove HTML from a string?"
132 Use HTML::Strip, or HTML::FormatText which not only removes HTML
133 but also attempts to do a little simple formatting of the resulting
134 plain text.
135 .SS "How do I extract URLs?"
136 .IX Subsection "How do I extract URLs?"
137 HTML::SimpleLinkExtor will extract URLs from HTML, it handles anchors,
138 images, objects, frames, and many other tags that can contain a URL.
139 If you need anything more complex, you can create your own subclass of
140 HTML::LinkExtor or HTML::Parser. You might even use
141 HTML::SimpleLinkExtor as an example for something specifically
142 suited to your needs.
143 .PP
144 You can use URI::Find or URL::Search to extract URLs from an
145 arbitrary text document.
146 .SS "How do I fetch an HTML file?"
147 .IX Subsection "How do I fetch an HTML file?"
148 (contributed by brian d foy)
149 .PP
150 The core HTTP::Tiny module can fetch web resources and give their
151 content back to you as a string:
152 .PP
153 .Vb 1
154 \& use HTTP::Tiny;
155 \&
156 \& my $ua = HTTP::Tiny\->new;
157 \& my $html = $ua\->get( "http://www.example.com/index.html" )\->{content};
158 .Ve
159 .PP
160 It can also store the resource directly in a file:
161 .PP
162 .Vb 1
163 \& $ua\->mirror( "http://www.example.com/index.html", "foo.html" );
164 .Ve
165 .PP
166 If you need to do something more complicated, the HTTP::Tiny object can
167 be customized by setting attributes, or you can use LWP::UserAgent from
168 the libwww-perl distribution or Mojo::UserAgent from the Mojolicious
169 distribution to make common tasks easier. If you want to simulate an
170 interactive web browser, you can use the WWW::Mechanize module.
171 .SS "How do I automate an HTML form submission?"
172 .IX Subsection "How do I automate an HTML form submission?"
173 If you are doing something complex, such as moving through many pages
174 and forms or a web site, you can use WWW::Mechanize. See its
175 documentation for all the details.
176 .PP
177 If you're submitting values using the GET method, create a URL and encode
178 the form using the \f(CW\*(C`www_form_urlencode\*(C'\fR method from HTTP::Tiny:
179 .PP
180 .Vb 1
181 \& use HTTP::Tiny;
182 \&
183 \& my $ua = HTTP::Tiny\->new;
184 \&
185 \& my $query = $ua\->www_form_urlencode([ q => \*(AqDB_File\*(Aq, lucky => 1 ]);
186 \& my $url = "https://metacpan.org/search?$query";
187 \& my $content = $ua\->get($url)\->{content};
188 .Ve
189 .PP
190 If you're using the POST method, the \f(CW\*(C`post_form\*(C'\fR method will encode the
191 content appropriately.
192 .PP
193 .Vb 1
194 \& use HTTP::Tiny;
195 \&
196 \& my $ua = HTTP::Tiny\->new;
197 \&
198 \& my $url = \*(Aqhttps://metacpan.org/search\*(Aq;
199 \& my $form = [ q => \*(AqDB_File\*(Aq, lucky => 1 ];
200 \& my $content = $ua\->post_form($url, $form)\->{content};
201 .Ve
202 .SS "How do I decode or create those %\-encodings on the web?"
203 .IX Xref "URI URI::Escape RFC 2396"
204 .IX Subsection "How do I decode or create those %-encodings on the web?"
205 Most of the time you should not need to do this as
206 your web framework, or if you are making a request,
207 the LWP or other module would handle it for you.
208 .PP
209 To encode a string yourself, use the URI::Escape module. The \f(CW\*(C`uri_escape\*(C'\fR
210 function returns the escaped string:
211 .PP
212 .Vb 1
213 \& my $original = "Colon : Hash # Percent %";
214 \&
215 \& my $escaped = uri_escape( $original );
216 \&
217 \& print "$escaped\en"; # \*(AqColon%20%3A%20Hash%20%23%20Percent%20%25\*(Aq
218 .Ve
219 .PP
220 To decode the string, use the \f(CW\*(C`uri_unescape\*(C'\fR function:
221 .PP
222 .Vb 1
223 \& my $unescaped = uri_unescape( $escaped );
224 \&
225 \& print $unescaped; # back to original
226 .Ve
227 .PP
228 Remember not to encode a full URI, you need to escape each
229 component separately and then join them together.
230 .SS "How do I redirect to another page?"
231 .IX Subsection "How do I redirect to another page?"
232 Most Perl Web Frameworks will have a mechanism for doing this,
233 using the Catalyst framework it would be:
234 .PP
235 .Vb 2
236 \& $c\->res\->redirect($url);
237 \& $c\->detach();
238 .Ve
239 .PP
240 If you are using Plack (which most frameworks do), then
241 Plack::Middleware::Rewrite is worth looking at if you
242 are migrating from Apache or have URL's you want to always
243 redirect.
244 .SS "How do I put a password on my web pages?"
245 .IX Subsection "How do I put a password on my web pages?"
246 See if the web framework you are using has an
247 authentication system and if that fits your needs.
248 .PP
249 Alternativly look at Plack::Middleware::Auth::Basic,
250 or one of the other Plack authentication <https://metacpan.org/search?q=plack+auth>
251 options.
252 .SS "How do I make sure users can't enter values into a form that causes my CGI script to do bad things?"
253 .IX Subsection "How do I make sure users can't enter values into a form that causes my CGI script to do bad things?"
254 (contributed by brian d foy)
255 .PP
256 You can't prevent people from sending your script bad data. Even if
257 you add some client-side checks, people may disable them or bypass
258 them completely. For instance, someone might use a module such as
259 LWP to submit to your web site. If you want to prevent data that
260 try to use SQL injection or other sorts of attacks (and you should
261 want to), you have to not trust any data that enter your program.
262 .PP
263 The perlsec documentation has general advice about data security.
264 If you are using the DBI module, use placeholder to fill in data.
265 If you are running external programs with \f(CW\*(C`system\*(C'\fR or \f(CW\*(C`exec\*(C'\fR, use
266 the list forms. There are many other precautions that you should take,
267 too many to list here, and most of them fall under the category of not
268 using any data that you don't intend to use. Trust no one.
269 .SS "How do I parse a mail header?"
270 .IX Subsection "How do I parse a mail header?"
271 Use the Email::MIME module. It's well-tested and supports all the
272 craziness that you'll see in the real world (comment-folding whitespace,
273 encodings, comments, etc.).
274 .PP
275 .Vb 1
276 \& use Email::MIME;
277 \&
278 \& my $message = Email::MIME\->new($rfc2822);
279 \& my $subject = $message\->header(\*(AqSubject\*(Aq);
280 \& my $from = $message\->header(\*(AqFrom\*(Aq);
281 .Ve
282 .PP
283 If you've already got some other kind of email object, consider passing
284 it to Email::Abstract and then using its cast method to get an
285 Email::MIME object:
286 .PP
287 .Vb 2
288 \& my $abstract = Email::Abstract\->new($mail_message_object);
289 \& my $email_mime_object = $abstract\->cast(\*(AqEmail::MIME\*(Aq);
290 .Ve
291 .SS "How do I check a valid mail address?"
292 .IX Subsection "How do I check a valid mail address?"
293 (partly contributed by Aaron Sherman)
294 .PP
295 This isn't as simple a question as it sounds. There are two parts:
296 .PP
297 a) How do I verify that an email address is correctly formatted?
298 .PP
299 b) How do I verify that an email address targets a valid recipient?
300 .PP
301 Without sending mail to the address and seeing whether there's a human
302 on the other end to answer you, you cannot fully answer part \fIb\fR, but
303 the Email::Valid module will do both part \fIa\fR and part \fIb\fR as far
304 as you can in real-time.
305 .PP
306 Our best advice for verifying a person's mail address is to have them
307 enter their address twice, just as you normally do to change a
308 password. This usually weeds out typos. If both versions match, send
309 mail to that address with a personal message. If you get the message
310 back and they've followed your directions, you can be reasonably
311 assured that it's real.
312 .PP
313 A related strategy that's less open to forgery is to give them a PIN
314 (personal ID number). Record the address and PIN (best that it be a
315 random one) for later processing. In the mail you send, include a link to
316 your site with the PIN included. If the mail bounces, you know it's not
317 valid. If they don't click on the link, either they forged the address or
318 (assuming they got the message) following through wasn't important so you
319 don't need to worry about it.
320 .SS "How do I decode a MIME/BASE64 string?"
321 .IX Subsection "How do I decode a MIME/BASE64 string?"
322 The MIME::Base64 package handles this as well as the MIME/QP encoding.
323 Decoding base 64 becomes as simple as:
324 .PP
325 .Vb 2
326 \& use MIME::Base64;
327 \& my $decoded = decode_base64($encoded);
328 .Ve
329 .PP
330 The Email::MIME module can decode base 64\-encoded email message parts
331 transparently so the developer doesn't need to worry about it.
332 .SS "How do I find the user's mail address?"
333 .IX Subsection "How do I find the user's mail address?"
334 Ask them for it. There are so many email providers available that it's
335 unlikely the local system has any idea how to determine a user's email address.
336 .PP
337 The exception is for organization-specific email (e.g. [email protected])
338 where policy can be codified in your program. In that case, you could look at
339 \&\f(CW$ENV\fR{USER}, \f(CW$ENV\fR{LOGNAME}, and getpwuid($<) in scalar context, like so:
340 .PP
341 .Vb 1
342 \& my $user_name = getpwuid($<)
343 .Ve
344 .PP
345 But you still cannot make assumptions about whether this is correct, unless
346 your policy says it is. You really are best off asking the user.
347 .SS "How do I send email?"
348 .IX Subsection "How do I send email?"
349 Use the Email::Stuffer module, like so:
350 .PP
351 .Vb 5
352 \& # first, create your message
353 \& my $message = Email::Stuffer\->from(\*([email protected]\*(Aq)
354 \& \->to(\*([email protected]\*(Aq)
355 \& \->subject(\*(AqHappy birthday!\*(Aq)
356 \& \->text_body("Happy birthday to you!\en");
357 \&
358 \& $message\->send_or_die;
359 .Ve
360 .PP
361 By default, Email::Sender::Simple (the \f(CW\*(C`send\*(C'\fR and \f(CW\*(C`send_or_die\*(C'\fR methods
362 use this under the hood) will try \f(CW\*(C`sendmail\*(C'\fR first, if it exists
363 in your \f(CW$PATH\fR. This generally isn't the case. If there's a remote mail
364 server you use to send mail, consider investigating one of the Transport
365 classes. At time of writing, the available transports include:
366 .IP Email::Sender::Transport::Sendmail 4
367 .IX Item "Email::Sender::Transport::Sendmail"
368 This is the default. If you can use the \fBmail\fR\|(1) or \fBmailx\fR\|(1)
369 program to send mail from the machine where your code runs, you should
370 be able to use this.
371 .IP Email::Sender::Transport::SMTP 4
372 .IX Item "Email::Sender::Transport::SMTP"
373 This transport contacts a remote SMTP server over TCP. It optionally
374 uses TLS or SSL and can authenticate to the server via SASL.
375 .PP
376 Telling Email::Stuffer to use your transport is straightforward.
377 .PP
378 .Vb 1
379 \& $message\->transport($email_sender_transport_object)\->send_or_die;
380 .Ve
381 .SS "How do I use MIME to make an attachment to a mail message?"
382 .IX Subsection "How do I use MIME to make an attachment to a mail message?"
383 Email::MIME directly supports multipart messages. Email::MIME
384 objects themselves are parts and can be attached to other Email::MIME
385 objects. Consult the Email::MIME documentation for more information,
386 including all of the supported methods and examples of their use.
387 .PP
388 Email::Stuffer uses Email::MIME under the hood to construct
389 messages, and wraps the most common attachment tasks with the simple
390 \&\f(CW\*(C`attach\*(C'\fR and \f(CW\*(C`attach_file\*(C'\fR methods.
391 .PP
392 .Vb 4
393 \& Email::Stuffer\->to(\*([email protected]\*(Aq)
394 \& \->subject(\*(AqThe file\*(Aq)
395 \& \->attach_file(\*(Aqstuff.csv\*(Aq)
396 \& \->send_or_die;
397 .Ve
398 .SS "How do I read email?"
399 .IX Subsection "How do I read email?"
400 Use the Email::Folder module, like so:
401 .PP
402 .Vb 1
403 \& use Email::Folder;
404 \&
405 \& my $folder = Email::Folder\->new(\*(Aq/path/to/email/folder\*(Aq);
406 \& while(my $message = $folder\->next_message) {
407 \& # next_message returns Email::Simple objects, but we want
408 \& # Email::MIME objects as they\*(Aqre more robust
409 \& my $mime = Email::MIME\->new($message\->as_string);
410 \& }
411 .Ve
412 .PP
413 There are different classes in the Email::Folder namespace for
414 supporting various mailbox types. Note that these modules are generally
415 rather limited and only support \fBreading\fR rather than writing.
416 .SS "How do I find out my hostname, domainname, or IP address?"
417 .IX Xref "hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa, gethostbyname, Socket, Net::Domain, Sys::Hostname"
418 .IX Subsection "How do I find out my hostname, domainname, or IP address?"
419 (contributed by brian d foy)
420 .PP
421 The Net::Domain module, which is part of the Standard Library starting
422 in Perl 5.7.3, can get you the fully qualified domain name (FQDN), the host
423 name, or the domain name.
424 .PP
425 .Vb 1
426 \& use Net::Domain qw(hostname hostfqdn hostdomain);
427 \&
428 \& my $host = hostfqdn();
429 .Ve
430 .PP
431 The Sys::Hostname module, part of the Standard Library, can also get the
432 hostname:
433 .PP
434 .Vb 1
435 \& use Sys::Hostname;
436 \&
437 \& $host = hostname();
438 .Ve
439 .PP
440 The Sys::Hostname::Long module takes a different approach and tries
441 harder to return the fully qualified hostname:
442 .PP
443 .Vb 1
444 \& use Sys::Hostname::Long \*(Aqhostname_long\*(Aq;
445 \&
446 \& my $hostname = hostname_long();
447 .Ve
448 .PP
449 To get the IP address, you can use the \f(CW\*(C`gethostbyname\*(C'\fR built-in function
450 to turn the name into a number. To turn that number into the dotted octet
451 form (a.b.c.d) that most people expect, use the \f(CW\*(C`inet_ntoa\*(C'\fR function
452 from the Socket module, which also comes with perl.
453 .PP
454 .Vb 1
455 \& use Socket;
456 \&
457 \& my $address = inet_ntoa(
458 \& scalar gethostbyname( $host || \*(Aqlocalhost\*(Aq )
459 \& );
460 .Ve
461 .SS "How do I fetch/put an (S)FTP file?"
462 .IX Subsection "How do I fetch/put an (S)FTP file?"
463 Net::FTP, and Net::SFTP allow you to interact with FTP and SFTP (Secure
464 FTP) servers.
465 .SS "How can I do RPC in Perl?"
466 .IX Subsection "How can I do RPC in Perl?"
467 Use one of the RPC modules( <https://metacpan.org/search?q=RPC> ).
468 .SH "AUTHOR AND COPYRIGHT"
469 .IX Header "AUTHOR AND COPYRIGHT"
470 Copyright (c) 1997\-2010 Tom Christiansen, Nathan Torkington, and
471 other authors as noted. All rights reserved.
472 .PP
473 This documentation is free; you can redistribute it and/or modify it
474 under the same terms as Perl itself.
475 .PP
476 Irrespective of its distribution, all code examples in this file
477 are hereby placed into the public domain. You are permitted and
478 encouraged to use this code in your own programs for fun
479 or for profit as you see fit. A simple comment in the code giving
480 credit would be courteous but is not required.