锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 闄凩earning Perl涓殑鐗囨璇存槑錛?br />
if ($name =~ /^Randal/) { 鍋滄帀apache鏄?br />
#cd /usr/local/apache2/bin
2
3 @list = <STDIN>;
4 @list = sort(@list);
5 @list = reverse @list;
6
7 print "\n";
8
9 print @list;
10
11 print "\n";
12 print "input the number:\n";
13 $num = <STDIN>;
14 print $list[$num];
15 srand;
16 print $list[rand(@list)];
鍙戠幇絎洓琛?@list[$num]鐨勭粨鏋滀篃鏄竴鏍風(fēng)殑錛屼笉蹇呮槸$list[$num]錛岄毦閬撴槸鐗堟湰闂錛?br />
涔︿笂璇磗rand鏄繘琛岄殢鏈烘暟鍒濆鍖栫殑錛屼絾鏄彂鐜板鏋滃垹闄よ繖琛屼篃鍙互
]]>
2 $filename = <STDIN>;
3 chomp $filename; # toss pesky newline
4 if (-r $filename && -w $filename) {
5 # file exists, and I can read and write it
6
7 }
Table 10.1: File Tests and Their Meanings
File Test Meaning
-r File or directory is readable
-w File or directory is writable
-x File or directory is executable
-o File or directory is owned by user
-R File or directory is readable by real user, not effective user (differs from -r for setuid
programs)
-W File or directory is writable by real user, not effective user (differs from -w for setuid
programs)
-X File or directory is executable by real user, not effective user (differs from -x for setuid
programs)
-O File or directory is owned by real user, not effective user (differs from -o for setuid
programs)
-e File or directory exists
-z File exists and has zero size (directories are never empty)
-s File or directory exists and has nonzero size (the value is the size in bytes)
-f Entry is a plain file
-d Entry is a directory
-l Entry is a symlink
-S Entry is a socket
-p Entry is a named pipe (a "fifo")
-b Entry is a block-special file (like a mountable disk)
-c Entry is a character-special file (like an I/O device)
-u File or directory is setuid
-g File or directory is setgid
-k File or directory has the sticky bit set
-t isatty() on the filehandle is true
-T File is "text"
-B File is "binary"
-M Modification age in days
-A Access age in days
-C Inode-modification age in days
2 print "$_ is readable\n" if -r; # same as -r $_
3 }
2.The stat and lstat Functions
2 $size,$atime,$mtime,$ctime,$blksize,$blocks) = stat()
]]>
2 print "$_[0], $_[1]!"n";
3 }
4 say("hello","world"); # hello world, once again
5 say("goodbye","cruel world"); # silent movie lament
6
2 my($n,@values); # create some local variables
3 ($n,@values) = @_; # split args into limit and values
4 my(@result); # temporary for holding the return value
5 foreach $_ (@values) { # step through the arg list
6 if ($_ > $n) { # is it eligible?
7 push(@result,$_); # add it
8 }
9 }
10 return @result; # return the final list
11 }
@this = bigger_than(5,1,5,15,30); # @this gets (15,30)
3.File-Level my( ) Variables
The my() operator can also be used at the outermost level of your program, outside of
any subroutines or blocks. While this isn't really a "local" variable in the sense
defined above, it's actually rather useful,especially when used in conjunction with a
Perl pragma:
use strict;
A pragma is a compiler directive. Other directives include those to set up integer
arithmetic, overload numeric operators, or request more verbose warnings and error
messages.
If you place this pragma at the beginning of your file, you will no longer be able
to use variables (scalars,arrays, and hashes) until you have first "declared" them.
And you declare them with my(), like so:
2 my $a; # starts as undef
3 my @b = qw(fred barney betty); # give initial value
4
5 push @b, qw(wilma); # cannot leave her out
6 @c = sort @b; # WILL NOT COMPILE
That last statement will be flagged at compile time as an error, because it referred
to a variable that had not previously been declared with my (that is, @c). In other
words,your program won't even start running unless every single variable being used
has been declared. The advantages of forcing variable declarations are twofold:
1. Your programs will run slightly faster (variables created with my are accessed
slightly faster than ordinary variables).
Variables created with my() are not found in any package.
2. You'll catch mistakes in typing much faster, because you'll no longer be able
to accidentally reference a nonexisting variable named $freed when you wanted $fred.
4.The last Statement
2 something;
3 something;
4 something;
5 if (somecondition) {
6 somethingorother;
7 somethingorother;
8 last; # break out of the while loop
9 }
10 morethings;
11 morethings;
12 }
13 # last comes here
2 firstpart;
3 firstpart;
4 firstpart;
5 if (somecondition) {
6 somepart;
7 somepart;
8 next;
9 }
10 otherpart;
11 otherpart;
12 # next comes here
13 }
6.The redo Statement
2 # redo comes here
3 something;
4 something;
5 something;
6 if (somecondition) {
7 somestuff;
8 somestuff;
9 redo;
10 }
11 morething;
12 morething;
13 morething;
14 }
7.Expression Modifiers
2 exp2 while exp1; # like: while (exp1) { exp2; }
3 exp2 until exp1; # like: until (exp1) { exp2; }
4 some_expression if control_expression; # like if (control_expression) { some_expression;}
8.&& and || as Control Structures
2 that if this; # another way
3
4 #Here's a third (and believe it or not, there are still others):
5 this && that;
6 #Likewise, the logical-or works like the unless statement (or unless modifier). So you can replace:
7 unless (this) { that; }
8 #with:
9 this || that;
9.Opening and Closing a Filehandle
2
3 open(OUT, ">outfile"); #open a file for writing
4
5 open(LOGFILE, ">>mylogfile"); #you can open a file for appending
6 close(LOGFILE);
10.A Slight Diversion: die
2 die "Sorry, I couldn't create /tmp/dataplace\n";
3
4 die "you gravy-sucking pigs"; #prints the file and line number
5
6 die "you gravy-sucking pigs\n"; #print either
7
8 open(LOG, ">>logfile") || die "cannot append: $!"; # $!: most recent operating system error
11.Using Filehandles
2 while (<EP>) {
3 chomp;
4 print "I saw $_ in the password file!\n";
5 }
6 close(EP);
2 print OUT "What's going on in this file";
3 close(OUT);
]]>
\b鎰忓懗鐫瀛楃杈圭晫銆備唬鐮侊細(xì)
print "what's your name?";
$name=<STDIN>;
chomp($name);
if($name=~ /^randl\b/i){
print "matched\n";
}else{
print "not matched\n";
}
濡傛灉杈撳叆randl*, *鏄瓧絎︺佹暟瀛楀拰涓嬪垝綰匡紝閭d箞閮戒細(xì)鎵撳嵃not matched
鑰岃緭鍏?randl%"銆?randl fa"絳夊垯matched銆?br />
/fred\b/; # matches fred, but not frederick
/\bmo/; # matches moe and mole, but not Elmo
/\bFred\b/; # matches Fred but not Frederick or alFred
/\b\+\b/; # matches "x+y" but not "++" or " + "
/abc\bdef/; # never matches (impossible for a boundary there)
word boundary鎸夊瓧闈㈡潵鐪嬫槸涓縐嶈竟鐣岋紝涓涓綅緗傚湪榪欎釜浣嶇疆涓よ竟鐨勫瓧絎︿竴涓笌\w鍖歸厤錛屼竴涓笌
\W鍖歸厤錛岃繖鏄竴縐嶆儏鍐碉紱榪樻湁涓縐嶆儏鍐靛氨鏄紝涓涓瓧絎︿笌\w鍖歸厤錛岃屽彟涓涓瓧絎﹀槢錛岃涔堟槸
瀛楃涓茬殑鎵撳ご錛岃涔堟槸緇撳熬銆傝孿b榪欎釜anchor鎭版伆闇瑕佸湪瀹冨嚭鐜扮殑浣嶇疆鏄竴涓獁ord boundary銆?br />
鍐嶇湅鏉?abc\bdef/錛孿b涓よ竟涓涓槸c錛屼竴涓槸d錛岄兘涓嶾w鍖歸厤錛岃繖宸蹭笉婊¤凍涓婅堪鐨勭涓縐嶆儏鍐碉紝
鑷充簬絎簩縐嶆儏鍐碉紝鏇村姞涓嶄細(xì)鏄瓧絎︿覆鐨勬墦澶存垨鏈熬浜?jiǎn)銆傛墍浠b鍑虹幇鐨勫湴鏂逛笉鍙兘鏄痺ord boundary銆?/p>
## yes, it matches
} else {
## no, it doesn't
}
Note that the regular expression is delimited by slashes. Within the slashes, spaces and other whitespace
are significant, just as they are within strings.
This almost does it, but it doesn't handle selecting randal or rejecting Randall. To accept randal,
we add the ignore-case option, a small i appended after the closing slash. To reject Randall, we add a
word boundary special marker (similar to vi and some versions of grep) in the form of "b in the regular
expression. This ensures that the character following the first l in the regular expression is not another
letter. This changes the regular expression to be /^randal"b/i, which means "randal at the
beginning of the string, no letter or digit following, and OK to be in either case."
]]>
cd DBI-1.607
perl Makefile.PL
make
make test
make install
DBD-myql-4.010涔熷悓鏍風(fēng)殑鏂瑰紡榪涜瀹夎銆?br />
3銆侀氳繃涓嬮潰鐨刾erl鑴氭湰榪炴帴鏁版嵁搴擄細(xì)
my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost","root","123456");
my $sth = $dbh->("select * from clients");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref())
{
print "cid=$ref->{'cid'} and cname=$ref->{'cname'}\n";
}
$sth->finish();
$dbh->disconnect();
]]>
1)鍒╃敤鍛戒護(hù)tar xvfz *.tar.gz灝哸pache瑙e帇.
2)cd httpd-2.2.11 3)鍒╃敤./configure --help鏌ョ湅甯姪淇℃伅,鐪嬬湅闇瑕侀厤緗摢浜涢夐」.鐢變簬鎴戣瑁卪ysql鍜宲hp,鎵浠ラ夋嫨浜?jiǎn)浠ヤ?br />
鍑犱釜閫夐」:./configure --prefix=/usr/local/apache2 --enable-rewrite --with-mpm=worker --enable-so
鏈鍚庝竴涓緢閲嶈,濡傛灉瑕佽php,涓瀹氳鍐?鍓嶉潰涓や釜涓嶆槸蹇呴渶鐨?<濡傛灉涓嶆槑鐧介夐」鐨勫惈涔?鍘籫oogle.>
4)make
5)make install
3. 鍚姩Apache錛?br />
#cd /usr/local/apache2/bin
#./apachectl start
#./apachectl stop
4.嫻嬭瘯瀹夎鏄惁鎴愬姛錛屾祻瑙堝櫒杈撳叆http://localhost, 欏甸潰鏄劇ずit works錛岃〃鏄巃pache瀹夎鎴愬姛.
5.嫻嬭瘯perl.鏂囦歡涓篶gitest.pl,鏀懼湪/usr/local/apache2/cgi-bin鐩綍涓紝騫朵笖chmod a+x cgitest.pl浣垮叾鍙墽琛?br />
錛?/p>
#!/usr/bin/perl -w
use CGI;
{
my $q = new CGI;
print $q->header(),
$q->start_html("hello,perl"),
$q->h1('hello perl'),
$q->end_html();
}
6.鍦ㄦ祻瑙堝櫒涓緭鍏ワ細(xì)http://localhost/cgi-bin/cgitest.pl 鍗沖彲鐪嬪埌hello perl銆?br />
7.鑷畾涔夋枃浠跺瓨鏀句綅緗細(xì)
# ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
# ScriptAlias /cgi/ "/usr/local/www/"
ScriptAlias / "/usr/local/www/"
#<Directory "/usr/local/apache2/cgi-bin">
<Directory "/usr/local/www">
榪欐牱灝卞彲浠ュ皢緗戦〉鏂囦歡瀛樻斁鍦?usr/local/www鐩綍
璁塊棶鐨勬椂鍊欙細(xì)http://localhost/cgitest.pl 鍗沖彲銆?nbsp;
while (<>) {
if (/abc/) {
print $_;
}
}
2.dot "." pattern
This matches any single character except newline (\n). For example, the pattern /a./ matches any two-letter sequence
that starts with a and is not "a\n".
3.A pattern-matching character class
open and close square brackets and a list of characters between the brackets. One and only one of these characters
must be present at the corresponding part of the string for the pattern to match. For example,
/[abcde]/
[0123456789] # match any single digit
[0-9] # same thing
[0-9\-] # match 0-9, or minus
[a-z0-9] # match any single lowercase letter or digit
[a-zA-Z0-9_] # match any single letter, digit, or underscore
There's also a negated character class, which is the same as a character class, but has a leading up-arrow
(or caret: ^) immediately after the left bracket. 錛坕f caret ^ is not right after the left bracket,then it means starting with錛塗his character class matches any single character that is
not in the list. For example:
[^0-9] # match any single non-digit
[^aeiouAEIOU] # match any single non-vowel
[^\^] # match single character except an up-arrow
Predefined Character Class Abbreviations
Construct Equivalent Class Negated Construct Equivalent Negated Class
\d (a digit) [0-9] \D (digits, not!) [^0-9]
\w (word char) [a-zA-Z0-9_] \W (words, not!) [^a-zA-Z0-9_]
\s (space char) [ \r\t\n\f] \S (space, not!) [^ \r\t\n\f]
4. Multipliers
asterisk (*) as a grouping pattern. The asterisk indicates zero or more of the
immediately previous character (or character class).
Two other grouping patterns that work like this are the plus sign (+), meaning one or more of the
immediately previous character, and the question mark (?), meaning zero or one of the immediately
previous character. For example, the regular expression /fo+ba?r/ matches an f followed by one or
more o's followed by a b, followed by an optional a, followed by an r.
the general multiplier. The general multiplier consists of a pair of matching curly braces with one or two numbers
inside, as in /x{5,10}/
We could dispense with *, +, and ? entirely, since they are completely equivalent to {0,}, {1,}, and
{0,1}. But it's easier to type the equivalent single punctuation character, and more familiar as well.
If two multipliers occur in a single expression, the greedy rule is augmented with "leftmost is greediest."
For example:
$_ = "a xxx c xxxxxxxx c xxx d";
/a.*c.*d/;
In this case, the first ".*" in the regular expression matches all characters up to the second c, even
though matching only the characters up to the first c would still allow the entire regular expression to
match. Right now, this doesn't make any difference (the pattern would match either way), but later when
we can look at parts of the regular expression that matched, it'll matter quite a bit.
We can force any multiplier to be nongreedy (or lazy) by following it with a question mark:
$_ = "a xxx c xxxxxxxx c xxx d";
/a.*?c.*d/;
Here, the a.*?c now matches the fewest characters between the a and c, not the most characters. This
means the leftmost c is matched, not the rightmost. You can put such a question-mark modifier after any
of the multipliers (?,+,*, and {m,n}).
What if the string and regular expression were slightly altered, say, to:
$_ = "a xxx ce xxxxxxxx ci xxx d";
/a.*ce.*d/;
In this case, if the .* matches the most characters possible before the next c, the next regular expression
character (e) doesn't match the next character of the string (i). In this case, we get automatic
backtracking: the multiplier is unwound and retried, stopping at someplace earlier (in this case, at the
earlier c, next to the e).[2] A complex regular expression may involve many such levels of backtracking,
leading to long execution times. In this case, making that match lazy (with a trailing "?") will actually
simplify the work that Perl has to perform, so you may want to consider that.
[2] Well, technically there was a lot of backtracking of the * operator to find the c's in the
first place. But that's a little trickier to describe, and it works on the same principle.
5.Parentheses as memory
To recall a memorized part of a string, you must include a backslash followed by an integer. This pattern
construct represents the same sequence of characters matched earlier in the same-numbered pair of
parentheses (counting from one). For example,
/fred(.)barney\1/;
matches a string consisting of fred, followed by any single non-newline character, followed by
barney, followed by that same single character. So, it matches fredxbarneyx, but not
fredxbarneyy. Compare that with
/fred.barney./;