1
Which one of the following is a major difference between the chop and chomp functions?
chomp and chop
Chomp cuts off only the current input record separator value; chop cuts off any character.
for example:
$a="abcde";
chomp($a);
chop($a);-------abcd
2 reverse
reverse 函数
例子:
print reverse <<EOF
hello WoRLD
This is teXt
EOF
结果:
DLEoW oLleh
tXet si sihT
this is tricky. Because scalar(reverse())
scalar ask reverse to be in scalar context. So reverse will reverse everything!!!
In list context,returns a list value consisting of the elements of LIST in the opposite order.
In scalar context,concatenates the elements of LIST and returns a string value with all characters in the opposite order.
print join(",",reverse "world","Hello"); # Hello,world?
print scalar reverse "dlrow,"olleH"; # Hello,world
First is List context. The second is scalar context.
It also interesting when you reverse a hash.
my %a = reverse %b;
See what happened?
the key->value changed to value->key.
If same value exists,only one of them kept.
my %a=('a'=>1,'b'=>2); my %b = reverse %a; foreach my $key(keys %b){ ? print "$key -> $b{$key}n"; }