首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

PERL语言几道题 请 2

2012-02-12 
PERL语言几道题请高手指点 2题目:显示当前路径的,能把那个 CMD ()FUNCTION做点更改么?内容:Write a prompt

PERL语言几道题 请高手指点 2
题目:显示当前路径的,能把那个 CMD ()FUNCTION做点更改么?
内容:
Write a prompting subroutine prompt that can be used in many Perl programs you'll write from now on. It should be called with one or two arguments: a prompting message and a default value for it, both specified by the user when calling the subroutine. If the user wants to accept the default value, she should just hit the <ENTER> key when prompted. The subroutine should return to the calling program what the user entered, i.e., the user input or the default value if user just pressed <ENTER>. Embed the source code for the prompt subroutine in a simple driver program called prompter.pl to test its functions, e.g., 
comp315@turing> prompter.pl 
Your turing name : despi
Your working directory [/home/despi]: c/docs
*** Your answers sent back to the caller:
despi, c/docs
comp315@turing> prompter.pl 
Your turing name : than2
Your working directory [/home/than2]: 
*** Your answers sent back to the caller:
than2, /home/than2

Notice that I used the output of the first prompter to build the default value for the second one but, in general, the two calls of the subroutine are independent and the default message can be anything. 

参考代码:(个人觉得有些不对,能把那个 CMD ()FUNCTION做点更改么)
use strict;
use warnings;
my $fullname;
my $pathname;
&prompt($fullname, $pathname);
print "*** Your answers sent back to the caller:\n";
print "$fullname, $pathname\n";
sub prompt{
  print "Your turing fullname : ";
  $fullname = <STDIN>;
  chomp($fullname);
  print "Your working pathname [/home/$fullname]: ";
  $pathname = <STDIN>;
  chomp($pathname);
  if($pathname =~ //){
  use Cwd;
  $pathname = cwd;
  }
  print "$fullname, $pathname\n";
  return($fullname, $pathname);
}



[解决办法]
问题在于对空pathname的match不对,if($pathname =~ //)总是返回true的,可以改为if($pathname =~ /^\s*$/)

热点排行