⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 503-505.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Perl</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=28//-->

<!--PAGES=503-505//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch27/500-502.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="505-507.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 28<BR>Perl

</FONT></H2>

<P><I>by Tim Parker</I></P>

<DL>

<DT><B>In This Chapter</B>

<DT>&#149;&nbsp;&nbsp; What is Perl?

<DT>&#149;&nbsp;&nbsp; Creating and executing Perl programs

<DT>&#149;&nbsp;&nbsp; Handling data in Perl

<DT>&#149;&nbsp;&nbsp; Arrays

<DT>&#149;&nbsp;&nbsp; Perl programming constructs

<DT>&#149;&nbsp;&nbsp; Functions

<DT>&#149;&nbsp;&nbsp; Perl operators

<DT>&#149;&nbsp;&nbsp; Converting programs to Perl

</DL>

<P>Perl, which stands for Practical Extraction and Report Language, is an interpreted programming language that was developed by Larry Wall. Perl was initially designed to make scanning and manipulating text files easier than it is in languages such as <TT>awk</TT>. This chapter includes the following topics:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;What Perl is

<DD><B>&#149;</B>&nbsp;&nbsp;Creating and executing Perl programs

<DD><B>&#149;</B>&nbsp;&nbsp;Data, variables, and arrays

<DD><B>&#149;</B>&nbsp;&nbsp;Perl programming constructs

<DD><B>&#149;</B>&nbsp;&nbsp;Perl functions

</DL>

<P>After reading this chapter, you should be familiar with some of the advantages that Perl offers over other programming languages. You should also be able to write simple Perl programs to help you in your day-to-day interactions with Linux.

</P>

<H3><A NAME="Heading2"></A><FONT COLOR="#000077">What Is Perl?</FONT></H3>

<P>As stated, Perl was originally created to make scanning and manipulating text files easier. People who are familiar with the <TT>awk</TT> programming language (see Chapter 25, &#147;<TT>gawk</TT>&#148;) will not be surprised to learn that Perl borrowed many of its features from <TT>awk</TT>. Perl also includes some of the best features of C, <TT>sed</TT>, and the UNIX shell languages such as <TT>bash</TT> and <TT>tcsh</TT>.</P>

<P>Perl is very similar to the shell programming languages both in syntax and in function. There are, however, a few differences worth mentioning. One of the biggest differences between how Perl works and how shell programs work is that Perl is not purely an interpreted language. Perl programs are actually read in full and stored in an intermediate form before they are executed. Shell programs are read and executed one command at a time. This offers two advantages for Perl over shell programs. First, Perl programs execute much faster than shell programs. This is because the Perl interpreter does all the syntax checking and stripping of comments before the code execution is started. Second, you don&#146;t have to worry about a large shell program stopping halfway through its execution as a result of a syntax error because all the code is parsed before it starts executing.</P>

<P>The fact that Perl is not a pure interpreted language also has one disadvantage. For small programs whose execution time is not that large, the extra compile time involved in using Perl actually makes it slower than using a shell language. This is a relatively small problem and one that few people actually notice.</P>

<H3><A NAME="Heading3"></A><FONT COLOR="#000077">Creating and Executing Perl Programs</FONT></H3>

<P>The steps you take to create and execute a Perl program are very similar to those used in creating and executing a Linux shell program. Perl programs consist of one or more Perl commands that are placed into a text file. The first thing you must do to create a Perl program is create a file in which to put Perl commands. You can do this using your text editor of choice. Let&#146;s start with a very simple example of a Perl program. The following Perl program, called <TT>hello</TT>, prints a greeting on the screen.</P>

<!-- CODE SNIP //-->

<PRE>

#!/usr/bin/perl

print &#147;Hi there!\n&#148;;

</PRE>

<!-- END CODE SNIP //-->

<P>There are several things worth mentioning about this example. The first is the strange-looking first line of the program. This line tells the shell program what program to run to execute the code contained in the file.

</P>

<P>The second line of the program prints <TT>Hi there!</TT>, followed by a new line on the screen. C programmers will notice how much this line looks like C code.</P>

<P>You have one more step to take before the program will actually work, and that is to change the permissions on the file so it is executable. This is done by entering the following command:</P>

<!-- CODE SNIP //-->

<PRE>

chmod &#43;x hello

</PRE>

<!-- END CODE SNIP //-->

<P>You can now run the program by typing the following on the command line:

</P>

<!-- CODE SNIP //-->

<PRE>

hello

</PRE>

<!-- END CODE SNIP //-->

<P>Running this program causes the Perl interpreter to be invoked. The Perl interpreter parses the whole program and then executes the compiled code.

</P>

<P>This is the standard way of running a Perl program. You can also run the <TT>hello</TT> program by invoking Perl on the command line and passing the <TT>hello</TT> code to it as a command-line parameter. This is done by entering the following command:</P>

<!-- CODE SNIP //-->

<PRE>

perl hello

</PRE>

<!-- END CODE SNIP //-->

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="../ch27/500-502.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="505-507.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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