您必须在输入末尾鞭打换行符:
#!/usr/bin/perl
print "Type Username Here:
";
chomp ( $username = <> );
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png
";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
您可以通过以下方式对变量进行清理(确保用户输入仅包含字母、数字和/或字符下划线,例如):_
#!/usr/bin/perl
print "Type Username Here:
";
$username = $1 if <> =~ /^([a-z0-9_]*)$/i;
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png
";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
写得更好(如@KeithThompson 评论):
#!/usr/bin/perl
print "Type Username Here:
";
if ( <> =~ /^([a-z0-9_]*)$/i ) {
$username = $1
print "Link: http://s3.amazonaws.com/MinecraftSkins/$username.png\n";
exec "start www.s3.amazonaws.com/MinecraftSkins/$username.png";
} else {
print "Error input contain invalid characters\n";
};