?? rhl29.htm
字號:
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens test.data for reading. File must exist.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"<test.data"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens test.data for reading. File must exist.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
">test.data"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens test.data for writing. Creates file if it does not exist. Appends to any existing file called test.data.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"+>test.data"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens test.data for reading and writing. Creates file if it does not exist.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"+<test.data"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens test.data for reading and writing. Creates file if it does not exist. Preferred way in Perl 5.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"| cmd"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens a pipe to write to.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"cmd |"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens a pipe to read from.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
"-"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens STDIN.</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
">-"
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Opens STDOUT.</FONT>
</TABLE><P>When working with multiple files, you can have more than one unique handle to write to or read from. Use the select HANDLE; call to set the default file handle to use when using print statements. For example, say you have two file handles, LARRY
and CURLY. Here's how to switch between handles:
<BR>
<PRE>
<FONT COLOR="#000080">select LARRY;
print "Whatsssa matter?\n"; # write to LARRY
select CURLY;
print "Whoop, whoop, whoop!"; # write to CURLY
select LARRY;
print "I oughta.... "; # write to LARRY again</FONT></PRE>
<P>Of course, by explicitly stating the handle name you could get the same result with these three lines of code:
<BR>
<PRE>
<FONT COLOR="#000080">print LARRY "Whatsssa matter?\n"; # write to LARRY
print CURLY "Whoop, whoop, whoop!"; # write to CURLY
print LARRY "I oughta.... "; # write to LARRY again</FONT></PRE>
<P>This is a very brief introduction to using file handles in Perl. The use of file handles is covered throughout the rest of the book, so don't worry if this pace of information is too quick. You will see plenty of examples throughout the book.
<BR>
<P>You can also check for the status of a file given a filename. The available tests are listed in the source test file in Listing 29.7.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 29.7. Testing file parameters.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">#!/usr/bin/perl
$name = "test.txt";
print "\nTesting flags for $name \n";
print "\n========== Effective User ID tests ";
print "\n is readable" if ( -r $name);
print "\n is writeable" if ( -w $name);
print "\n is executeable" if ( -x $name);
print "\n is owned " if ( -o $name);
print "\n========== Real User ID tests ";
print "\n is readable" if ( -R $name);
print "\n is writeable" if ( -W $name);
print "\n is executeable" if ( -X $name);
print "\n is owned " if ( -O $name);
print "\n========== Reality Checks ";
print "\n exists " if ( -e $name);
print "\n has zero size " if ( -z $name);
print "\n has some bytes in it " if ( -s $name);
print "\n is a file " if (-f $name);
print "\n is a directory " if (-d $name);
print "\n is a link " if (-l $name);
print "\n is a socket " if (-S $name);
print "\n is a pipe " if (-p $name);
print "\n is a block device " if (-b $name);
print "\n is a character device " if (-c $name);
print "\n has setuid bit set " if (-u $name);
print "\n has sticky bit set " if (-k $name);
print "\n has gid bit set " if (-g $name);
print "\n is open to terminal " if (-t $name);
print "\n is a Binary file " if (-B $name);
print "\n is a Text file " if (-T $name);
print "\n is Binary to terminal " if (-t $name);
print "\n is open to terminal " if (-t $name);
print "\n age of file is ".(int(-M $name)+1)." day(s) old" if (-e $name);print "\n access time of file is ".(-A $name) if (-e $name);
print "\n inode change time of file is ".(-C $name) if (-e $name);
printf "\n";</FONT></PRE>
<BR>
<A NAME="E68E234"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Working with Patterns</B></FONT></CENTER></H3>
<BR>
<P>Perl has a very powerful regular expression parser and string search and replace functions. To search for a substring, you would use the following syntax normally within an if block:
<BR>
<PRE>
<FONT COLOR="#000080">if ($a =~ /menu/) {
printf "\n Found menu in $a! \n";
}</FONT></PRE>
<P>The value in $a is the number of matched strings. To search in a case-insensitive manner, use an 'i' at the end of the search statement like this:
<BR>
<PRE>
<FONT COLOR="#000080">if ($a =~ /mEnU/i) {
printf "\n Found menu in $a! \n";
}</FONT></PRE>
<P>You can even search for items in an array. For example, if $a was an array @a, then the returned value from the search operation will be an array with all the matched strings. If you do not specify the "@a =~" portion, then Perl will use the
$_ default name space to search on.
<BR>
<P>To search and replace strings, use the following syntax:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">$expr =~ s/old/new/gie</FONT></PRE>
<P>The 'g', 'i', and 'e' are optional parameters. If the 'g' is not specified, only the first match to the "old" string will be replaced with "new." The 'i' flag specifies a case-insensitive search. The 'e' forces Perl to use the
"new" string as a Perl expression. So, in the following example, the value of $a will be "HIGHWAY":
<BR>
<PRE>
<FONT COLOR="#000080">$a = "DRIVEWAY";
$a =~ s/HIGH/DRIVE/
print $a;</FONT></PRE>
<P>Perl has a grep() function very similar to the grep function in UNIX, Perl's grep function takes a regular expression and a list. The return value from grep can be handled one of two ways: if assigned to a scalar, it is the number of matches found, and
if assigned to a list, it's a sublist of all the items found via grep.
<BR>
<P>Please check the man pages for using grep(). Some of the main types of predefined patterns are shown in Table 29.5.
<BR>
<BR>
<P ALIGN=CENTER>
<CENTER>
<FONT COLOR="#000080"><B>Table 29.5. Main types of predefined patterns.</B></FONT></CENTER>
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Code </I>
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
<I>Pattern</I></FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Any character</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\d
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Digits [0-9]</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\D
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Anything but digits</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\w
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
[a-zA-Z]</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\W
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Anything but \w</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\s
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Space or tab</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\S
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Anything but \s</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\n
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Newline</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\r
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Carriage return</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\t
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Tab</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\f
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Formfeed</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\0
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Null</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\000
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Octal</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\X00
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Hex</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
\cX
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
ASCII Control Character</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
*
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Zero or more of previous pattern</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
+
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
One or more of previous pattern</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
?
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Zero or one of previous pattern</FONT>
</TABLE><P>Perl uses the special variable called $_. This is the default variable to use by Perl if you do not explicitly specify a variable name and Perl expects a variable. For example, in the grep() function, if you omit the LIST, grep() will use the
string in the variable $_. The $_ variable is Perl's default string to search, assign input, or read for data for a number.
<BR>
<BR>
<A NAME="E68E235"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Subroutines</B></FONT></CENTER></H3>
<BR>
<P>Perl 5 supports subroutines and functions with the sub command. You can use pointers to subroutines, too. The syntax for subroutines is
<BR>
<PRE>
<FONT COLOR="#000080">sub Name {
}</FONT></PRE>
<P>The ending curly brace does not require a semicolon to terminate it. If you are using a reference to a subroutine it can be declared without a Name, as shown here:
<BR>
<PRE>
<FONT COLOR="#000080">$ptr = sub {
};</FONT></PRE>
<P>Note the use of the <!--(semicolons):Perl subroutines;):Perl subroutines;-->semicolon to terminate the end of the subroutine. To call this function you would use the following line:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">&\$ptr(argument list);</FONT></PRE>
<P>Parameters to subroutines are passed in the @_ array. To get the individual items in the array, we can use $_[0], $_[1], and so on. You can define your own local variables with the 'local' keyword.
<BR>
<PRE>
<FONT COLOR="#000080">sub sample {
local ($a, $b, @c, $x) = @_
&lowerFunc();
}</FONT></PRE>
<P>In the preceding subroutine, you will find that $a = $_[0], $b = $_[1] and @c will point to the rest of the arguments as one list with $x empty. Generally, an array is the last assignment in such an assignment since it chews up all your parameters.
<BR>
<P>The 'local' variables will all be available for use in the lowerFunc() function. To hide the $a, $b, @c, and $x from lowerFunc, use the 'my' keyword like this:
<BR>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -