appb_05.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 328 行 · 第 1/2 页

HTM
328
字号
die "Couldn't get the size of the image"  unless defined $fred_height;</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.8" /><div class="sect2"><h3 class="sect2">B.5.8. The Net::SMTP Module</h3><p>If you want your program to be able to send<a name="INDEX-1176" />email through an<a name="INDEX-1177" />SMTP server (which is the way most ofus send email these days, whether you knew that or not), you may usethe <tt class="literal">Net::SMTP</tt><a name="INDEX-1178" /> module to do the work.<a href="#FOOTNOTE-405">[405]</a> This module, which isavailable from CPAN, is object-oriented, but you may simply followthe syntax to use it. You will need to change the name of your SMTPhost and the other items to make this work on your system. Yoursystem administrator or local expert can tell you what to use. Forexample:</p><blockquote class="footnote"><a name="FOOTNOTE-405" /><p>[405]Yes, this means that you are now able to use Perl to send spam.Please don't. </p> </blockquote><blockquote><pre class="code">use Net::SMTP;my $from = 'YOUR_ADDRESS_GOES_HERE';         # maybe fred@bedrock.edumy $site = 'YOUR_SITE_NAME_GOES_HERE';       # maybe bedrock.edumy $smtp_host = 'YOUR_SMTP_HOST_GOES_HERE';  # maybe mail or mailhostmy $to = 'president@whitehouse.gov';my $smtp = Net::SMTP-&gt;new($smtp_host, Hello =&gt; $site);$smtp-&gt;mail($from);$smtp-&gt;to($to);$smtp-&gt;data( );$smtp-&gt;datasend("To: $to\n");$smtp-&gt;datasend("Subject: A message from my Perl program.\n");$smtp-&gt;datasend("\n");$smtp-&gt;datasend("This is just to let you know,\n");$smtp-&gt;datasend("I don't care what those other people say about you,\n");$smtp-&gt;datasend("I still think you're doing a great job.\n");$smtp-&gt;datasend("\n");$smtp-&gt;datasend("Have you considered enacting a law naming Perl \n");$smtp-&gt;datasend("the national programming language?\n");$smtp-&gt;dataend( );                             # Not datasend!$smtp-&gt;quit;</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.9" /><div class="sect2"><h3 class="sect2">B.5.9. The POSIX Module</h3><p>If you need access to the POSIX (IEEE Std 1003.1) functions, the<tt class="literal">POSIX</tt><a name="INDEX-1179" /> module is for you. It provides manyfunctions that C programmers may be used to, such as trigonometricfunctions (<tt class="literal">asin</tt>, <tt class="literal">cosh</tt>), generalmathematical functions (<tt class="literal">floor</tt>,<tt class="literal">frexp</tt>), character-identification functions(<tt class="literal">isupper</tt>, <tt class="literal">isalpha</tt>), low-levelIO functions (<tt class="literal">creat</tt>, <tt class="literal">open</tt>), andsome others (<tt class="literal">asctime</tt>, <tt class="literal">clock</tt>).You'll probably want to call each of these with its"full" name; that is, with <tt class="literal">POSIX</tt> and apair of colons as a prefix to the function's name:</p><blockquote><pre class="code">use POSIX;print "Please enter a number: ";chomp(my $str = &lt;STDIN&gt;);$! = 0;  # Clear out the error indicatormy($num, $leftover) = POSIX::strtod($str);if ($str eq '') {  print "That string was empty!\n";} elsif ($leftover) {  my $remainder = substr $str, -$leftover;  print "The string '$remainder' was left after the number $num.\n";} elsif ($!) {  print "The conversion function complained: $!\n";} else {  print "The seemingly-valid number was $num.\n";}</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.10" /><div class="sect2"><h3 class="sect2">B.5.10. The Sys::Hostname Module</h3><p>The<tt class="literal">Sys::Hostname</tt><a name="INDEX-1180" /> module provides the<tt class="literal">hostname</tt><a name="INDEX-1181" /> function, which will be the networkname of your machine, if that can be determined. (If it can'tbe determined, perhaps because your machine is not on the Internet ornot properly configured, the function will die automatically;there's no point in using <tt class="literal">or die</tt> here.) Forexample:</p><blockquote><pre class="code">use Sys::Hostname;my $host = hostname;print "This machine is known as '$host'.\n";</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.11" /><div class="sect2"><h3 class="sect2">B.5.11. The Text::Wrap Module</h3><p>The <tt class="literal">Text::Wrap</tt><a name="INDEX-1182" /><a name="INDEX-1183" /> module supplies the<tt class="literal">wrap</tt><a name="INDEX-1184" /> function, which lets you implementsimple <a name="INDEX-1185" />word-wrapping. The firsttwo parameters specify the indentation of the first line and theothers, respectively; the remaining parameters make up theparagraph's text:</p><blockquote><pre class="code">use Text::Wrap;my $message = "This is some sample text which may be longer " .  "than the width of your output device, so it needs to " .  "be wrapped to fit properly as a paragraph. ";$message x= 5;print wrap("\t", "", "$message\n");</pre></blockquote></div><a name="lperl3-APP-B-SECT-5.12" /><div class="sect2"><h3 class="sect2">B.5.12. The Time::Local Module</h3><p><a name="INDEX-1186" />If you have a time (for example, from the<tt class="literal">time</tt> function) that needs to be converted to alist of year, month, day, hour, minute, and second values, you can dothat with Perl's built-in <tt class="literal">localtime</tt> functionin a list context.<a href="#FOOTNOTE-406">[406]</a> (Ina scalar context, that gives a nicely formatted string representingthe time, which is more often what you'd want.) But if you needto go in the other direction, you may use the<tt class="literal">timelocal</tt><a name="INDEX-1187" /> function from the<tt class="literal">Time::Local</tt> module instead. It's importantto note that the value of <tt class="literal">$mon</tt> and<tt class="literal">$year</tt> for January 2004 are not<tt class="literal">1</tt> and <tt class="literal">2004</tt> as you might expect,so be sure to read the documentation before you use this module. Forexample:</p><blockquote class="footnote"> <a name="FOOTNOTE-406" /><p>[406]The actual return value of<tt class="literal">localtime</tt> in a list context is a little differentthan you might expect; see the documentation.</p> </blockquote><a name="INDEX-1188" /><blockquote><pre class="code">use Time::Local;my $time = timelocal($sec, $min, $hr, $day, $mon, $year);</pre></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="appb_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="appb_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">B.4. Extending Perl's Functionality</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">B.6. Pragmas</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?