#!/usr/bin/perl
#Script:name2.pl - Demonstrates use of chop
#
print("Enter your name:");
$name=<STDIN>;
$lname = chop($name); #Removes newline character from $name
if ($lname ne "") {
print("$lname,have a nice day\n");
} else {
print("You have not entered your name\n");
}
$ ./name2.pl
Enter your name:stallman
,have a nice day
$
执行 Perl 程序时,它不执行“chop”函数。
编程线路是否有错误?
答案1
看劈
砍掉字符串的最后一个字符并返回被砍掉的字符。
即它就地修改字符串。
您假设它返回“切碎的字符串”。
#!/usr/bin/perl -w
my $x = "Turing, Alana";
my $r = chop($x);
print "\$x is now '$x', and \$r is now '$r'\n";
然后:
$x is now 'Turing, Alan', and $r is now 'a'