1、Perl的Hello World版,vi编辑文件hello,在此文件中输入以下内容:
1 #!/usr/bin/perl
2 print ("Hello, world!\n");
即打印Hello World!
2、变量定义:
1 #!/usr/bin/perl -w
2 print "What is your name? ";
3 $name = <STDIN>; #定义一个变量采用符号$
4 chomp ($name); #删除本字符串最后的"\n",即回车符
5 print "Hello, $name!\n";
3、在perl中,只区分字符串和数字这两种数据类型,而且两者之间还可以互换。字符串分单引号字符串和
双引号字符串,数字分整型和浮点型。
4、string repetition operator:
"fred" x 3 # is "fredfredfred"
5、"." operator
"hello" . "world" # same as "helloworld"
6、chop and chomp Functions
1 $x = "hello world";
2 chop($x); # $x is now "hello worl"
3
4 $a = "hello world\n";
5 chomp ($a); # $a is now "hello world"
6 chomp ($a); # aha! no change in $a
7、variable interpolation:
1 $a = "fred";
2 $b = "some text $a"; # $b is now "some text fred"
3
4 $fred = 'hi';
5 $barney = "a test of " . '$fred'; # literally: 'a test of $fred'
6 $barney2= "a test of \$fred"; # same thing
8、list literal
1,2,3) # array of three values 1, 2, and 3
("fred",4.5) # two values, "fred" and 4.5
9、list constructor operator
(1 .. 5) # same as (1, 2, 3, 4, 5)
(1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2)
(1.3 .. 6.1) # same as (1.3,2.3,3.3,4.3,5.3)
10、"quote word" function
@a = ("fred","barney","betty","wilma"); # ugh!
@a = qw(fred barney betty wilma); # better!
11、Array Element Access
@fred = (7,8,9);
$b = $fred[0]; # give 7 to $b (first element of @fred)
$fred[0] = 5; # now @fred = (5,8,9)
@fred[0,1]; # same as ($fred[0],$fred[1])
12、push and pop Functions
@mylist = (1,2,3);
push(@mylist,4,5,6); # @mylist = (1,2,3,4,5,6)
pop(@mylist); # @mylist = (1,2,3,4,5);
13、shift and unshift Functions
@fred = (5,6,7);
unshift(@fred,2,3,4); # @fred is now (2,3,4,5,6,7)
$x = shift(@fred); # $x gets 2, @fred is now (3,4,5,6,7)
14、reverse Function
@a = (7,8,9);
@b = reverse(@a); # gives @b the value of (9,8,7)
@b = reverse(7,8,9); # same thing
14.1 <STDIN> as an Array:
@a = <STDIN>; # read standard input in a list context
15、The if/unless Statement,unless其实就是if not
1 print "how old are you? ";
2 $a = <STDIN>;
3 chomp($a);
4 unless ($a < 18) {
5 print "Old enough! Cool! So go vote!\n";
6 $voter++;
7 }
16、true or false:
In Perl, the rules are slightly weird, but they give you the expected
results. The control expression is evaluated for a string value in scalar
context (if it's already a string, no change, but if it's a number, it is converted
to a string).If this string is either the empty string (with a length of zero), or a
string consisting of the single character "0" (the digit zero), then the value of
the expression is false. Anything else is true automatically.Some examples below:
0 # converts to "0", so false
1-1 # computes to 0, then converts to "0", so false
1 # converts to "1", so true
"" # empty string, so false
"1" # not "" or "0", so true
"00" # not "" or "0", so true (this is weird, watch out)
"0.000" # also true for the same reason and warning
undef # evaluates to "", so false
17、elsif(not elseif or elif)
1 if (some_expression_one) {
2 one_true_statement_1;
3 } elsif (some_expression_two) {
4 two_true_statement_1;
5 } else {
6 all_false_statement_1;
7 }
18、while/until Statement
1 $stops = 0;
2 do {
3 $stops++;
4 print "Next stop? ";
5 chomp($location = <STDIN>);
6 } until $stops > 5 || $location eq 'home';
19、for Statement
for ( initial_exp; test_exp; re-init_exp ) {
statement_1;
}
20、foreach Statement
@a = (1,2,3,4,5);
foreach $b (reverse @a) {
print $b;
}
or
foreach (reverse @a) {
print; # $_ variable is used as a default for many of Perl's operations
}
21、Hash
%smooth = ("aaa","bbb","234.5",456.7);
22、keys Function
$fred{"aaa"} = "bbb";
$fred{234.5} = 456.7;
@list = keys(%fred); # @list gets ("aaa",234.5) or (234.5,"aaa")
23、values function
%lastname = (); # force %lastname empty
$lastname{"fred"} = "flintstone";
$lastname{"barney"} = "rubble";
@lastnames = values(%lastname); # grab the values
24、each Function
while (($first,$last) = each(%lastname)) {
print "The last name of $first is $last\n";
}
25、delete Function
%fred = ("aaa","bbb",234.5,34.56); # give %fred two elements
delete $fred{"aaa"}; //# %fred is now just one key-value pair
26、Hash Slices
@score{"fred","barney","dino"} = (205,195,30);
Hash slices can also be used to merge a smaller hash into a larger one. In this example,
the smaller hash takes precedence in the sense that if there are duplicate keys,
the value from the smaller hash is used:
%league{keys %score} = values %score;
Here, the values of %score are merged into the %league hash. This is equivalent to
the much slower operation:
%league = (%league, %score); # merge %score into %league
27、Input from STDIN
$a = <STDIN>; # read the next line
@a = <STDIN>;
Typically, one thing you want to do is read all lines one at a time and do something
with each line. One common way to do this is:
while (defined($line = <STDIN>)) {
# process $line here
}
28、Diamond Operator
If you don't specify any filenames on the command line, the diamond operator reads
from standard input automatically.
#!/usr/bin/perl
while (<>) {
print $_;
}
or
@ARGV = ("aaa","bbb","ccc");
while (<>) { # process files aaa, bbb, and ccc
print "this line is: $_"; //print every line in these files aaa,bbb and ccc
}
29、printf for Formatted Output
printf "%15s %5d %10.2f\n", $s, $n, $r;
30、if (($words{$somename} || "groucho") eq $someguess) {
return 1; # return value is true
注意上面的||不是或运算符,而是说如果左边的值为空的话,那么就是用右边的值。