2 tricks in perl programming
时间:2006-10-17 来源:megazeng
A. You can find in many perl books say ”If you are a C developer or Java developer, you will definitely think that a function or method should add "return" keyword if the fuction or method is required to return values, But in perl, actually, it is not required.“ Let's see the example 1 below:
#!/usr/bin/perl -w |
All right, return is not used and 55 is printed out. But let's see another example 2:
#!/usr/bin/perl -w |
Guess will the above code work ???
......
ACTUALLY, the value in the if block will never return if we do not provide the key word "return". Is it interesting ???
B. As we know that if we want to call a sub routine , we will need to use "&" before the name of the sub routine. But if the sub rountine is defined before the code which calls this sub rountine, "&" can be omitted. See the below example 3:
#/usr/bin/perl -w |
But you should pay attention on the sub routine name, if the name is the same as the perl built in sub routine name, the sub rountine you defined will never been called. see example 4 below :
#/usr/bin/perl -w
sub chomp { |
Cool ??? :)