Chinaunix首页 | 论坛 | 博客
  • 博客访问: 738867
  • 博文数量: 235
  • 博客积分: 4309
  • 博客等级: 中校
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-17 11:25
个人简介

If you don\\\\\\\\\\\\\\\'t wanna do it, you find an EXCUSE; if you do, you\\\\\\\\\\\\\\\'ll find a WAY :-)

文章分类

全部博文(235)

文章存档

2014年(3)

2013年(2)

2012年(31)

2011年(199)

分类: Python/Ruby

2011-01-17 20:52:15

1>: Input from Standard Input
chomp($line = );
(Since the line-input operator will return undef when you reach end-of-file, this is handy
for dropping out of loops)
while (defined($line = )) {
print "I saw $line";
}
(Use the defined function, which returns false for undef, and true for everything else)

while () {
print "I saw $_"; # same as above
}
In the while loop, Perl reads a line of input, puts it into a variable, and runs the body of the loop. 
Then, it goes back to find another line of input.

foreach () {
print "I saw $_";
}
In the foreach loop, the line-input operator is being used in a list context
So it has to read all of the input before the loop can start running.

2> : Input from the Diamond Operator
$ ./my_program fred barney betty

while (defined($line = <>)) {
chomp($line);
print "It was $line that I saw!\n";
}

while (<>) {
chomp; # This works like the loop above, but with less typing
print "It was $_ that I saw!\n";
}

(The Invocation Arguments)
@ARGV is already stuffed full of the list of invocation arguments
@ARGV = qw# larry moe curly #; # force these three files to be read
while (<>) {
chomp;
print "It was $_ that I saw in some stooge-like file!\n";
}

3>: Output to Standard Output
@array=qw/ fred barney betty /;
print @array; # fredbarneybetty
print "@array"; # fred barney betty

$result = print("hello world!\n"); # result get 1,indicating the success of the print
print (2+3)*4; # print 5
(print (2+3))*4; # Oops!

4>: Formatted Output with printf
printf "Hello, %s; your password expires in %d days!\n", $user, $days_to_die;
%d:means a decimal integer
 %f:means a floating-point
 %g:(General)  automatically chooses floating-point,integer,or even exponential notation
 %s:means a string
%%:to print a real percent sign
printf "%g %g %g\n", 5/2, 51/17, 51 ** 17;

printf "in %d days!\n", 17.85; # in 17 days (Note that this is truncated, not rounded)
printf "m\n", 42; # output like ````42  (the ` symbol stands for a space)
printf "-\n", 2e3 + 1.95; # 2001

printf "s\n", "wilma"; # (right-justified) looks like `````wilma
printf "%-15s\n", "flintstone"; # (left-justified) looks like flintstone`````

printf "f\n", 6 * 7 + 2/3; # looks like ```42.666667
printf ".3f\n", 6 * 7 + 2/3; # looks like ``````42.667
printf ".0f\n", 6 * 7 + 2/3; # looks like ``````````43

printf "Monthly interest rate: %.2f%%\n", 5.25/12; # the value looks like "0.44%"

5>: Arrays and printf
my @items = qw( wilma dino pebbles );
printf "The items are: \n" . ("s\n" x @items), @items;
(Note that here we have @items being used once in a scalar context, to get its length, and
once in a list context, to get its contents. Context is important.)

6>: Filehandle
(the recommendation from Larry is that you
use all uppercase letters in the name of your filehandle)
STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUT
阅读(510) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~