Chinaunix首页 | 论坛 | 博客

  • 博客访问: 897014
  • 博文数量: 226
  • 博客积分: 10077
  • 博客等级: 上将
  • 技术积分: 2708
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-25 14:36
文章分类

全部博文(226)

文章存档

2010年(15)

2009年(82)

2008年(129)

我的朋友

分类:

2009-11-30 11:49:59

Unix 用户非常熟悉基于文本的 UI 模型。设想有一个 Perl 程序,让我们先看一下这个模型用于该程序的简单实现。标准的 Getopt::Std 模块简化了命令行参数的解析。这个程序仅仅为了说明 Getopt::Std 模块(没有实际用途)。 请参阅本文后面的参考资料。

				

#!/usr/bin/perl -w

use strict; # always use strict, it's a good habit
use Getopt::Std; # see "perldoc Getopt::Std"

my %options;
getopts('f:hl', \%options); # read the options with getopts

# uncomment the following two lines to see what the options hash contains
#use Data::Dumper;
#print Dumper \%options;

$options{h} && usage(); # the -h switch

# use the -f switch, if it's given, or use a default configuration filename
my $config_file = $options{f} || 'first.conf';

print "Configuration file is $config_file\n";

# check for the -l switch
if ($options{l})
{
system('/bin/ls -l');
}
else
{
system('/bin/ls');
}

# print out the help and exit
sub usage
{
print <first.pl [-l] [-h] [-f FILENAME]

Lists the files in the current directory, using either /bin/ls or
/bin/ls -l. The -f switch selects a different configuration file.
The -h switch prints this help.
EOHIPPUS
exit;
}

阅读(1251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~