Commit | Line | Data |
68dc0745 |
1 | =head1 NAME |
2 | |
d92eb7b0 |
3 | perlfaq9 - Networking ($Revision: 1.26 $, $Date: 1999/05/23 16:08:30 $) |
68dc0745 |
4 | |
5 | =head1 DESCRIPTION |
6 | |
7 | This section deals with questions related to networking, the internet, |
8 | and a few on the web. |
9 | |
a6dd486b |
10 | =head2 My CGI script runs from the command line but not the browser. (500 Server Error) |
68dc0745 |
11 | |
c8db1d39 |
12 | If you can demonstrate that you've read the following FAQs and that |
13 | your problem isn't something simple that can be easily answered, you'll |
14 | probably receive a courteous and useful reply to your question if you |
15 | post it on comp.infosystems.www.authoring.cgi (if it's something to do |
16 | with HTTP, HTML, or the CGI protocols). Questions that appear to be Perl |
17 | questions but are really CGI ones that are posted to comp.lang.perl.misc |
18 | may not be so well received. |
68dc0745 |
19 | |
c8db1d39 |
20 | The useful FAQs and related documents are: |
68dc0745 |
21 | |
c8db1d39 |
22 | CGI FAQ |
6cecdcac |
23 | http://www.webthing.com/tutorials/cgifaq.html |
68dc0745 |
24 | |
c8db1d39 |
25 | Web FAQ |
92c2ed05 |
26 | http://www.boutell.com/faq/ |
68dc0745 |
27 | |
c8db1d39 |
28 | WWW Security FAQ |
29 | http://www.w3.org/Security/Faq/ |
30 | |
31 | HTTP Spec |
32 | http://www.w3.org/pub/WWW/Protocols/HTTP/ |
33 | |
34 | HTML Spec |
35 | http://www.w3.org/TR/REC-html40/ |
36 | http://www.w3.org/pub/WWW/MarkUp/ |
37 | |
38 | CGI Spec |
39 | http://www.w3.org/CGI/ |
40 | |
41 | CGI Security FAQ |
42 | http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt |
43 | |
44 | =head2 How can I get better error messages from a CGI program? |
45 | |
46 | Use the CGI::Carp module. It replaces C<warn> and C<die>, plus the |
47 | normal Carp modules C<carp>, C<croak>, and C<confess> functions with |
48 | more verbose and safer versions. It still sends them to the normal |
49 | server error log. |
50 | |
51 | use CGI::Carp; |
52 | warn "This is a complaint"; |
53 | die "But this one is serious"; |
54 | |
55 | The following use of CGI::Carp also redirects errors to a file of your choice, |
56 | placed in a BEGIN block to catch compile-time warnings as well: |
57 | |
58 | BEGIN { |
59 | use CGI::Carp qw(carpout); |
60 | open(LOG, ">>/var/local/cgi-logs/mycgi-log") |
61 | or die "Unable to append to mycgi-log: $!\n"; |
62 | carpout(*LOG); |
63 | } |
64 | |
65 | You can even arrange for fatal errors to go back to the client browser, |
66 | which is nice for your own debugging, but might confuse the end user. |
67 | |
68 | use CGI::Carp qw(fatalsToBrowser); |
69 | die "Bad error here"; |
70 | |
71 | Even if the error happens before you get the HTTP header out, the module |
72 | will try to take care of this to avoid the dreaded server 500 errors. |
73 | Normal warnings still go out to the server error log (or wherever |
74 | you've sent them with C<carpout>) with the application name and date |
75 | stamp prepended. |
76 | |
68dc0745 |
77 | =head2 How do I remove HTML from a string? |
78 | |
f29c64d6 |
79 | The most correct way (albeit not the fastest) is to use HTML::Parser |
bed171df |
80 | from CPAN. Another mostly correct |
7d7e76cf |
81 | way is to use HTML::FormatText which not only removes HTML but also |
82 | attempts to do a little simple formatting of the resulting plain text. |
68dc0745 |
83 | |
84 | Many folks attempt a simple-minded regular expression approach, like |
c47ff5f1 |
85 | C<< s/<.*?>//g >>, but that fails in many cases because the tags |
68dc0745 |
86 | may continue over line breaks, they may contain quoted angle-brackets, |
a6dd486b |
87 | or HTML comment may be present. Plus, folks forget to convert |
88 | entities--like C<<> for example. |
68dc0745 |
89 | |
90 | Here's one "simple-minded" approach, that works for most files: |
91 | |
92 | #!/usr/bin/perl -p0777 |
93 | s/<(?:[^>'"]*|(['"]).*?\1)*>//gs |
94 | |
95 | If you want a more complete solution, see the 3-stage striphtml |
96 | program in |
97 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/striphtml.gz |
98 | . |
99 | |
c8db1d39 |
100 | Here are some tricky cases that you should think about when picking |
101 | a solution: |
102 | |
103 | <IMG SRC = "foo.gif" ALT = "A > B"> |
104 | |
d92eb7b0 |
105 | <IMG SRC = "foo.gif" |
c8db1d39 |
106 | ALT = "A > B"> |
107 | |
108 | <!-- <A comment> --> |
109 | |
110 | <script>if (a<b && a>c)</script> |
111 | |
112 | <# Just data #> |
113 | |
114 | <![INCLUDE CDATA [ >>>>>>>>>>>> ]]> |
115 | |
116 | If HTML comments include other tags, those solutions would also break |
117 | on text like this: |
118 | |
119 | <!-- This section commented out. |
120 | <B>You can't see me!</B> |
121 | --> |
122 | |
68dc0745 |
123 | =head2 How do I extract URLs? |
124 | |
54310121 |
125 | A quick but imperfect approach is |
68dc0745 |
126 | |
127 | #!/usr/bin/perl -n00 |
128 | # qxurl - tchrist@perl.com |
129 | print "$2\n" while m{ |
130 | < \s* |
131 | A \s+ HREF \s* = \s* (["']) (.*?) \1 |
132 | \s* > |
133 | }gsix; |
134 | |
135 | This version does not adjust relative URLs, understand alternate |
d92eb7b0 |
136 | bases, deal with HTML comments, deal with HREF and NAME attributes |
137 | in the same tag, understand extra qualifiers like TARGET, or accept |
138 | URLs themselves as arguments. It also runs about 100x faster than a |
139 | more "complete" solution using the LWP suite of modules, such as the |
140 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/xurl.gz program. |
68dc0745 |
141 | |
142 | =head2 How do I download a file from the user's machine? How do I open a file on another machine? |
143 | |
144 | In the context of an HTML form, you can use what's known as |
145 | B<multipart/form-data> encoding. The CGI.pm module (available from |
146 | CPAN) supports this in the start_multipart_form() method, which isn't |
147 | the same as the startform() method. |
148 | |
149 | =head2 How do I make a pop-up menu in HTML? |
150 | |
c47ff5f1 |
151 | Use the B<< <SELECT> >> and B<< <OPTION> >> tags. The CGI.pm |
68dc0745 |
152 | module (available from CPAN) supports this widget, as well as many |
153 | others, including some that it cleverly synthesizes on its own. |
154 | |
155 | =head2 How do I fetch an HTML file? |
156 | |
46fc3d4c |
157 | One approach, if you have the lynx text-based HTML browser installed |
158 | on your system, is this: |
68dc0745 |
159 | |
160 | $html_code = `lynx -source $url`; |
161 | $text_data = `lynx -dump $url`; |
162 | |
d92eb7b0 |
163 | The libwww-perl (LWP) modules from CPAN provide a more powerful way |
164 | to do this. They don't require lynx, but like lynx, can still work |
165 | through proxies: |
46fc3d4c |
166 | |
c8db1d39 |
167 | # simplest version |
168 | use LWP::Simple; |
169 | $content = get($URL); |
170 | |
171 | # or print HTML from a URL |
46fc3d4c |
172 | use LWP::Simple; |
6cecdcac |
173 | getprint "http://www.linpro.no/lwp/"; |
46fc3d4c |
174 | |
c8db1d39 |
175 | # or print ASCII from HTML from a URL |
65acb1b1 |
176 | # also need HTML-Tree package from CPAN |
46fc3d4c |
177 | use LWP::Simple; |
f29c64d6 |
178 | use HTML::Parser; |
46fc3d4c |
179 | use HTML::FormatText; |
180 | my ($html, $ascii); |
181 | $html = get("http://www.perl.com/"); |
182 | defined $html |
183 | or die "Can't fetch HTML from http://www.perl.com/"; |
184 | $ascii = HTML::FormatText->new->format(parse_html($html)); |
185 | print $ascii; |
186 | |
c8db1d39 |
187 | =head2 How do I automate an HTML form submission? |
188 | |
189 | If you're submitting values using the GET method, create a URL and encode |
190 | the form using the C<query_form> method: |
191 | |
192 | use LWP::Simple; |
193 | use URI::URL; |
194 | |
195 | my $url = url('http://www.perl.com/cgi-bin/cpan_mod'); |
196 | $url->query_form(module => 'DB_File', readme => 1); |
197 | $content = get($url); |
198 | |
199 | If you're using the POST method, create your own user agent and encode |
200 | the content appropriately. |
201 | |
202 | use HTTP::Request::Common qw(POST); |
203 | use LWP::UserAgent; |
204 | |
205 | $ua = LWP::UserAgent->new(); |
206 | my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod', |
207 | [ module => 'DB_File', readme => 1 ]; |
208 | $content = $ua->request($req)->as_string; |
209 | |
210 | =head2 How do I decode or create those %-encodings on the web? |
68dc0745 |
211 | |
68dc0745 |
212 | |
575cc754 |
213 | If you are writing a CGI script, you should be using the CGI.pm module |
214 | that comes with perl, or some other equivalent module. The CGI module |
215 | automatically decodes queries for you, and provides an escape() |
216 | function to handle encoding. |
68dc0745 |
217 | |
575cc754 |
218 | |
219 | The best source of detailed information on URI encoding is RFC 2396. |
220 | Basically, the following substitutions do it: |
221 | |
222 | s/([^\w()'*~!.-])/sprintf '%%%02x', $1/eg; # encode |
223 | |
224 | s/%([A-Fa-f\d]{2})/chr hex $1/eg; # decode |
225 | |
226 | However, you should only apply them to individual URI components, not |
227 | the entire URI, otherwise you'll lose information and generally mess |
228 | things up. If that didn't explain it, don't worry. Just go read |
229 | section 2 of the RFC, it's probably the best explanation there is. |
230 | |
231 | RFC 2396 also contains a lot of other useful information, including a |
232 | regexp for breaking any arbitrary URI into components (Appendix B). |
68dc0745 |
233 | |
234 | =head2 How do I redirect to another page? |
235 | |
fd5506a0 |
236 | According to RFC 2616, "Hypertext Transfer Protocol -- HTTP/1.1", the |
237 | preferred method is to send a C<Location:> header instead of a |
238 | C<Content-Type:> header: |
68dc0745 |
239 | |
240 | Location: http://www.domain.com/newpage |
68dc0745 |
241 | |
242 | Note that relative URLs in these headers can cause strange effects |
243 | because of "optimizations" that servers do. |
244 | |
c8db1d39 |
245 | $url = "http://www.perl.com/CPAN/"; |
246 | print "Location: $url\n\n"; |
247 | exit; |
248 | |
d92eb7b0 |
249 | To target a particular frame in a frameset, include the "Window-target:" |
250 | in the header. |
251 | |
252 | print <<EOF; |
253 | Location: http://www.domain.com/newpage |
254 | Window-target: <FrameName> |
255 | |
256 | EOF |
257 | |
a6dd486b |
258 | To be correct to the spec, each of those virtual newlines should |
259 | really be physical C<"\015\012"> sequences by the time your message is |
260 | received by the client browser. Except for NPH scripts, though, that |
261 | local newline should get translated by your server into standard form, |
262 | so you shouldn't have a problem here, even if you are stuck on MacOS. |
263 | Everybody else probably won't even notice. |
c8db1d39 |
264 | |
68dc0745 |
265 | =head2 How do I put a password on my web pages? |
266 | |
267 | That depends. You'll need to read the documentation for your web |
268 | server, or perhaps check some of the other FAQs referenced above. |
269 | |
270 | =head2 How do I edit my .htpasswd and .htgroup files with Perl? |
271 | |
272 | The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide a |
273 | consistent OO interface to these files, regardless of how they're |
46fc3d4c |
274 | stored. Databases may be text, dbm, Berkley DB or any database with a |
68dc0745 |
275 | DBI compatible driver. HTTPD::UserAdmin supports files used by the |
276 | `Basic' and `Digest' authentication schemes. Here's an example: |
277 | |
278 | use HTTPD::UserAdmin (); |
279 | HTTPD::UserAdmin |
280 | ->new(DB => "/foo/.htpasswd") |
281 | ->add($username => $password); |
282 | |
46fc3d4c |
283 | =head2 How do I make sure users can't enter values into a form that cause my CGI script to do bad things? |
284 | |
285 | Read the CGI security FAQ, at |
a6dd486b |
286 | http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html , and the |
46fc3d4c |
287 | Perl/CGI FAQ at |
a6dd486b |
288 | http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html . |
46fc3d4c |
289 | |
290 | In brief: use tainting (see L<perlsec>), which makes sure that data |
291 | from outside your script (eg, CGI parameters) are never used in |
292 | C<eval> or C<system> calls. In addition to tainting, never use the |
293 | single-argument form of system() or exec(). Instead, supply the |
294 | command and arguments as a list, which prevents shell globbing. |
295 | |
5a964f20 |
296 | =head2 How do I parse a mail header? |
68dc0745 |
297 | |
298 | For a quick-and-dirty solution, try this solution derived |
b73a15ae |
299 | from L<perlfunc/split>: |
68dc0745 |
300 | |
301 | $/ = ''; |
302 | $header = <MSG>; |
303 | $header =~ s/\n\s+/ /g; # merge continuation lines |
304 | %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header ); |
305 | |
306 | That solution doesn't do well if, for example, you're trying to |
307 | maintain all the Received lines. A more complete approach is to use |
308 | the Mail::Header module from CPAN (part of the MailTools package). |
309 | |
310 | =head2 How do I decode a CGI form? |
311 | |
c8db1d39 |
312 | You use a standard module, probably CGI.pm. Under no circumstances |
313 | should you attempt to do so by hand! |
314 | |
315 | You'll see a lot of CGI programs that blindly read from STDIN the number |
316 | of bytes equal to CONTENT_LENGTH for POSTs, or grab QUERY_STRING for |
317 | decoding GETs. These programs are very poorly written. They only work |
318 | sometimes. They typically forget to check the return value of the read() |
319 | system call, which is a cardinal sin. They don't handle HEAD requests. |
320 | They don't handle multipart forms used for file uploads. They don't deal |
321 | with GET/POST combinations where query fields are in more than one place. |
322 | They don't deal with keywords in the query string. |
323 | |
324 | In short, they're bad hacks. Resist them at all costs. Please do not be |
325 | tempted to reinvent the wheel. Instead, use the CGI.pm or CGI_Lite.pm |
326 | (available from CPAN), or if you're trapped in the module-free land |
327 | of perl1 .. perl4, you might look into cgi-lib.pl (available from |
65acb1b1 |
328 | http://cgi-lib.stanford.edu/cgi-lib/ ). |
c8db1d39 |
329 | |
330 | Make sure you know whether to use a GET or a POST in your form. |
331 | GETs should only be used for something that doesn't update the server. |
332 | Otherwise you can get mangled databases and repeated feedback mail |
333 | messages. The fancy word for this is ``idempotency''. This simply |
334 | means that there should be no difference between making a GET request |
335 | for a particular URL once or multiple times. This is because the |
336 | HTTP protocol definition says that a GET request may be cached by the |
337 | browser, or server, or an intervening proxy. POST requests cannot be |
338 | cached, because each request is independent and matters. Typically, |
339 | POST requests change or depend on state on the server (query or update |
340 | a database, send mail, or purchase a computer). |
68dc0745 |
341 | |
5a964f20 |
342 | =head2 How do I check a valid mail address? |
68dc0745 |
343 | |
c8db1d39 |
344 | You can't, at least, not in real time. Bummer, eh? |
68dc0745 |
345 | |
c8db1d39 |
346 | Without sending mail to the address and seeing whether there's a human |
347 | on the other hand to answer you, you cannot determine whether a mail |
348 | address is valid. Even if you apply the mail header standard, you |
349 | can have problems, because there are deliverable addresses that aren't |
350 | RFC-822 (the mail header standard) compliant, and addresses that aren't |
351 | deliverable which are compliant. |
68dc0745 |
352 | |
c8db1d39 |
353 | Many are tempted to try to eliminate many frequently-invalid |
d92eb7b0 |
354 | mail addresses with a simple regex, such as |
b8c8cfe2 |
355 | C</^[\w.-]+\@(?:[\w-]+\.)+\w+$/>. It's a very bad idea. However, |
c8db1d39 |
356 | this also throws out many valid ones, and says nothing about |
b8c8cfe2 |
357 | potential deliverability, so it is not suggested. Instead, see |
358 | http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz, |
68dc0745 |
359 | which actually checks against the full RFC spec (except for nested |
5a964f20 |
360 | comments), looks for addresses you may not wish to accept mail to |
68dc0745 |
361 | (say, Bill Clinton or your postmaster), and then makes sure that the |
c8db1d39 |
362 | hostname given can be looked up in the DNS MX records. It's not fast, |
363 | but it works for what it tries to do. |
364 | |
365 | Our best advice for verifying a person's mail address is to have them |
366 | enter their address twice, just as you normally do to change a password. |
367 | This usually weeds out typos. If both versions match, send |
368 | mail to that address with a personal message that looks somewhat like: |
369 | |
370 | Dear someuser@host.com, |
371 | |
372 | Please confirm the mail address you gave us Wed May 6 09:38:41 |
373 | MDT 1998 by replying to this message. Include the string |
374 | "Rumpelstiltskin" in that reply, but spelled in reverse; that is, |
375 | start with "Nik...". Once this is done, your confirmed address will |
376 | be entered into our records. |
377 | |
378 | If you get the message back and they've followed your directions, |
379 | you can be reasonably assured that it's real. |
68dc0745 |
380 | |
c8db1d39 |
381 | A related strategy that's less open to forgery is to give them a PIN |
382 | (personal ID number). Record the address and PIN (best that it be a |
383 | random one) for later processing. In the mail you send, ask them to |
384 | include the PIN in their reply. But if it bounces, or the message is |
385 | included via a ``vacation'' script, it'll be there anyway. So it's |
386 | best to ask them to mail back a slight alteration of the PIN, such as |
387 | with the characters reversed, one added or subtracted to each digit, etc. |
46fc3d4c |
388 | |
68dc0745 |
389 | =head2 How do I decode a MIME/BASE64 string? |
390 | |
6a0af2f1 |
391 | The MIME-Base64 package (available from CPAN) handles this as well as |
392 | the MIME/QP encoding. Decoding BASE64 becomes as simple as: |
68dc0745 |
393 | |
6a0af2f1 |
394 | use MIME::Base64; |
68dc0745 |
395 | $decoded = decode_base64($encoded); |
396 | |
26d9b02f |
397 | The MIME-Tools package (available from CPAN) supports extraction with |
6a0af2f1 |
398 | decoding of BASE64 encoded attachments and content directly from email |
399 | messages. |
400 | |
401 | If the string to decode is short (less than 84 bytes long) |
402 | a more direct approach is to use the unpack() function's "u" |
68dc0745 |
403 | format after minor transliterations: |
404 | |
405 | tr#A-Za-z0-9+/##cd; # remove non-base64 chars |
406 | tr#A-Za-z0-9+/# -_#; # convert to uuencoded format |
407 | $len = pack("c", 32 + 0.75*length); # compute length byte |
408 | print unpack("u", $len . $_); # uudecode and print |
409 | |
5a964f20 |
410 | =head2 How do I return the user's mail address? |
68dc0745 |
411 | |
a6dd486b |
412 | On systems that support getpwuid, the $< variable, and the |
68dc0745 |
413 | Sys::Hostname module (which is part of the standard perl distribution), |
414 | you can probably try using something like this: |
415 | |
416 | use Sys::Hostname; |
231ab6d1 |
417 | $address = sprintf('%s@%s', scalar getpwuid($<), hostname); |
68dc0745 |
418 | |
5a964f20 |
419 | Company policies on mail address can mean that this generates addresses |
420 | that the company's mail system will not accept, so you should ask for |
421 | users' mail addresses when this matters. Furthermore, not all systems |
68dc0745 |
422 | on which Perl runs are so forthcoming with this information as is Unix. |
423 | |
424 | The Mail::Util module from CPAN (part of the MailTools package) provides a |
425 | mailaddress() function that tries to guess the mail address of the user. |
426 | It makes a more intelligent guess than the code above, using information |
427 | given when the module was installed, but it could still be incorrect. |
428 | Again, the best way is often just to ask the user. |
429 | |
c8db1d39 |
430 | =head2 How do I send mail? |
68dc0745 |
431 | |
c8db1d39 |
432 | Use the C<sendmail> program directly: |
433 | |
434 | open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") |
435 | or die "Can't fork for sendmail: $!\n"; |
436 | print SENDMAIL <<"EOF"; |
437 | From: User Originating Mail <me\@host> |
438 | To: Final Destination <you\@otherhost> |
439 | Subject: A relevant subject line |
440 | |
65acb1b1 |
441 | Body of the message goes here after the blank line |
442 | in as many lines as you like. |
c8db1d39 |
443 | EOF |
444 | close(SENDMAIL) or warn "sendmail didn't close nicely"; |
445 | |
446 | The B<-oi> option prevents sendmail from interpreting a line consisting |
447 | of a single dot as "end of message". The B<-t> option says to use the |
448 | headers to decide who to send the message to, and B<-odq> says to put |
449 | the message into the queue. This last option means your message won't |
450 | be immediately delivered, so leave it out if you want immediate |
451 | delivery. |
452 | |
d92eb7b0 |
453 | Alternate, less convenient approaches include calling mail (sometimes |
454 | called mailx) directly or simply opening up port 25 have having an |
455 | intimate conversation between just you and the remote SMTP daemon, |
456 | probably sendmail. |
457 | |
458 | Or you might be able use the CPAN module Mail::Mailer: |
c8db1d39 |
459 | |
460 | use Mail::Mailer; |
461 | |
462 | $mailer = Mail::Mailer->new(); |
463 | $mailer->open({ From => $from_address, |
464 | To => $to_address, |
465 | Subject => $subject, |
466 | }) |
467 | or die "Can't open: $!\n"; |
468 | print $mailer $body; |
469 | $mailer->close(); |
470 | |
471 | The Mail::Internet module uses Net::SMTP which is less Unix-centric than |
472 | Mail::Mailer, but less reliable. Avoid raw SMTP commands. There |
d92eb7b0 |
473 | are many reasons to use a mail transport agent like sendmail. These |
c8db1d39 |
474 | include queueing, MX records, and security. |
475 | |
575cc754 |
476 | =head2 How do I use MIME to make an attachment to a mail message? |
477 | |
478 | This answer is extracted directly from the MIME::Lite documentation. |
479 | Create a multipart message (i.e., one with attachments). |
480 | |
481 | use MIME::Lite; |
482 | |
483 | ### Create a new multipart message: |
484 | $msg = MIME::Lite->new( |
485 | From =>'me@myhost.com', |
486 | To =>'you@yourhost.com', |
487 | Cc =>'some@other.com, some@more.com', |
488 | Subject =>'A message with 2 parts...', |
489 | Type =>'multipart/mixed' |
490 | ); |
491 | |
492 | ### Add parts (each "attach" has same arguments as "new"): |
493 | $msg->attach(Type =>'TEXT', |
494 | Data =>"Here's the GIF file you wanted" |
495 | ); |
496 | $msg->attach(Type =>'image/gif', |
497 | Path =>'aaa000123.gif', |
498 | Filename =>'logo.gif' |
499 | ); |
500 | |
501 | $text = $msg->as_string; |
502 | |
503 | MIME::Lite also includes a method for sending these things. |
504 | |
505 | $msg->send; |
506 | |
507 | This defaults to using L<sendmail(1)> but can be customized to use |
508 | SMTP via L<Net::SMTP>. |
509 | |
c8db1d39 |
510 | =head2 How do I read mail? |
511 | |
d92eb7b0 |
512 | While you could use the Mail::Folder module from CPAN (part of the |
513 | MailFolder package) or the Mail::Internet module from CPAN (also part |
a6dd486b |
514 | of the MailTools package), often a module is overkill. Here's a |
d92eb7b0 |
515 | mail sorter. |
516 | |
517 | #!/usr/bin/perl |
c8db1d39 |
518 | # bysub1 - simple sort by subject |
519 | my(@msgs, @sub); |
520 | my $msgno = -1; |
521 | $/ = ''; # paragraph reads |
522 | while (<>) { |
523 | if (/^From/m) { |
524 | /^Subject:\s*(?:Re:\s*)*(.*)/mi; |
525 | $sub[++$msgno] = lc($1) || ''; |
526 | } |
527 | $msgs[$msgno] .= $_; |
d92eb7b0 |
528 | } |
c8db1d39 |
529 | for my $i (sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msgs)) { |
530 | print $msgs[$i]; |
531 | } |
532 | |
d92eb7b0 |
533 | Or more succinctly, |
c8db1d39 |
534 | |
535 | #!/usr/bin/perl -n00 |
536 | # bysub2 - awkish sort-by-subject |
537 | BEGIN { $msgno = -1 } |
538 | $sub[++$msgno] = (/^Subject:\s*(?:Re:\s*)*(.*)/mi)[0] if /^From/m; |
539 | $msg[$msgno] .= $_; |
540 | END { print @msg[ sort { $sub[$a] cmp $sub[$b] || $a <=> $b } (0 .. $#msg) ] } |
541 | |
68dc0745 |
542 | =head2 How do I find out my hostname/domainname/IP address? |
543 | |
c8db1d39 |
544 | The normal way to find your own hostname is to call the C<`hostname`> |
545 | program. While sometimes expedient, this has some problems, such as |
546 | not knowing whether you've got the canonical name or not. It's one of |
547 | those tradeoffs of convenience versus portability. |
68dc0745 |
548 | |
549 | The Sys::Hostname module (part of the standard perl distribution) will |
550 | give you the hostname after which you can find out the IP address |
551 | (assuming you have working DNS) with a gethostbyname() call. |
552 | |
553 | use Socket; |
554 | use Sys::Hostname; |
555 | my $host = hostname(); |
65acb1b1 |
556 | my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); |
68dc0745 |
557 | |
558 | Probably the simplest way to learn your DNS domain name is to grok |
559 | it out of /etc/resolv.conf, at least under Unix. Of course, this |
560 | assumes several things about your resolv.conf configuration, including |
561 | that it exists. |
562 | |
563 | (We still need a good DNS domain name-learning method for non-Unix |
564 | systems.) |
565 | |
566 | =head2 How do I fetch a news article or the active newsgroups? |
567 | |
568 | Use the Net::NNTP or News::NNTPClient modules, both available from CPAN. |
a6dd486b |
569 | This can make tasks like fetching the newsgroup list as simple as |
68dc0745 |
570 | |
571 | perl -MNews::NNTPClient |
572 | -e 'print News::NNTPClient->new->list("newsgroups")' |
573 | |
574 | =head2 How do I fetch/put an FTP file? |
575 | |
576 | LWP::Simple (available from CPAN) can fetch but not put. Net::FTP (also |
577 | available from CPAN) is more complex but can put as well as fetch. |
578 | |
579 | =head2 How can I do RPC in Perl? |
580 | |
a6dd486b |
581 | A DCE::RPC module is being developed (but is not yet available) and |
68dc0745 |
582 | will be released as part of the DCE-Perl package (available from |
65acb1b1 |
583 | CPAN). The rpcgen suite, available from CPAN/authors/id/JAKE/, is |
584 | an RPC stub generator and includes an RPC::ONC module. |
68dc0745 |
585 | |
586 | =head1 AUTHOR AND COPYRIGHT |
587 | |
65acb1b1 |
588 | Copyright (c) 1997-1999 Tom Christiansen and Nathan Torkington. |
5a964f20 |
589 | All rights reserved. |
590 | |
591 | When included as part of the Standard Version of Perl, or as part of |
592 | its complete documentation whether printed or otherwise, this work |
d92eb7b0 |
593 | may be distributed only under the terms of Perl's Artistic License. |
5a964f20 |
594 | Any distribution of this file or derivatives thereof I<outside> |
595 | of that package require that special arrangements be made with |
596 | copyright holder. |
597 | |
598 | Irrespective of its distribution, all code examples in this file |
599 | are hereby placed into the public domain. You are permitted and |
600 | encouraged to use this code in your own programs for fun |
601 | or for profit as you see fit. A simple comment in the code giving |
602 | credit would be courteous but is not required. |