来源:

http://www.cnblogs.com/itech/archive/2012/08/07/2627267.html

代码:

需要显式地定义变量且初始化。例如optionX。

如果没有定义变量且显式初始化,且没有在命令行指定选项,则选项对应的变量将为未定义。

 #!/bin/perl-5.8.3/bin/perl$
use warnings;
use strict; use Data::Dumper;
use Getopt::Long;
use Pod::Usage; our $g_opts;
our $optionX=''; #if not defined in command line, it will be empty string
sub parse_opts{
my $result = GetOptions(
"optionA=s" => \$g_opts->{'optionA'},#string
"optionB=s" => \$g_opts->{'optionB'},#string
"optionC=i" => \$g_opts->{'optionC'},#integer
"optionD=f" => \$g_opts->{'optionD'},#float
"optionX=f" => \$optionX,
"optionY=f" => \$optionY,
"verbose" => \$g_opts->{'verbose'},#flag
"quiet" => sub { $g_opts->{'verbose'} = },
"help|?" => \$g_opts->{'help'}
);
if(!($g_opts->{'optionA'})){
&pod2usage( -verbose => );#exit status will be 1
}
if($g_opts->{'help'}){
&pod2usage( -verbose => );#exit status will be 1
}
} &parse_opts();
print("\n$optionX\n");
print($optionY); #if not defined in command line, it will be undefined
print($g_opts->{"optionB"}); foreach my $key (keys %{$g_opts}){
if(!$g_opts->{$key}) {next;}
print($key . "=" . $g_opts->{$key} . "\n");
}
exit(); __END__ =head1 NAME
sample - Using Getopt::Long and Pod::Usage
=head1 SYNOPSIS
sample [options] [args ...]
Options: -optionA optionA
-optionB optionB
-optionC optionC
-optionD optionD
-verbose verbose
-quiet noverbose
-help brief help message
=head1 OPTIONS
=over =item B<-help> Print a brief help message and exits. =back
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do something
useful with the contents thereof. =cut

perl的Getopt::Long和pod::usage ?的更多相关文章

  1. Perl 模块 Getopt::Std 和 Getopt::Long

    示例程序: getopt.pl; 1 2 3 4 5 6 7 8 #!/usr/bin/perl -w #use strict; use Getopt::Std; use vars qw($opt_a ...

  2. MySQL高可用方案MHA的部署和原理

    MHA(Master High Availability)是一套相对成熟的MySQL高可用方案,能做到在0~30s内自动完成数据库的故障切换操作,在master服务器不宕机的情况下,基本能保证数据的一 ...

  3. mysql MHA架构搭建过程

    [环境介绍] 系统环境:Red Hat Enterprise Linux 7 + 5.7.18 + MHA version 0.57 系统 IP 主机名 备注 版本 xx系统 192.168.142. ...

  4. Orchestrator+Proxysql 实现自动导换+应用透明读写分离

    前面一篇博客我介绍了如何通过Orachestrator+脚本实现自动导换. 这一章,我来演示一下如何通过Orchestrator+Proxysql 实现自动导换+应用透明读写分离 总体架构 可选架构1 ...

  5. [Perl] Getopt 函数来接收用户参数的使用

    我们在linux常常用到一个程序需要加入参数,现在了解一下perl中的有关控制参数的函数.getopt.在linux有的参数有二种形式.一种是–help,另一种是-h.也就是-和–的分别.–表示完整参 ...

  6. python module getopt usage

    import getopt import sys def usage(): print 'this is a usage.' def main(): try: print sys.argv #sys. ...

  7. Perl:写POD文档

    官方手册:https://perldoc.perl.org/perlpod.html POD文档是perl的man文档,可以用perldoc输出,也可以直接用man输出.在开始下面的文章之前,请先粗略 ...

  8. awk,perl,python的命令行参数处理

    Python,Perl,Bash命令行参数 Part I 日常经常性的和Perl,Python,Bash打交道,但是又经常性的搞混他们之间,在命令行上的特殊性和index的区别,Python真的是人性 ...

  9. perl 里面如何写出阅读友好的代码提示

    在我们使用别人写好的程序时,经常会使用-h 之类的东西查看一下简单的帮助手册或者说明信息: 对于perl 语言而言,写起来简单,经常随手一写,解决了当时的问题,但是过几天去看,你都不知道这个脚本该怎么 ...

随机推荐

  1. 解决 GoogleApi 无法访问的问题

    因为 google 被天朝屏蔽,所以很多运用了 fonts.googleapis 的网站都打开很慢,会直到加载 fonts.googleapis 超时才能打开网页. 在本地开发时,可以引用国内的CDN ...

  2. Android View的事件分发

    如果接触android开发时间足够长的话,或多或少都会遇到各种各样事件冲突的问题,要想解决这类问题,对深入理解事件分发机制是很有必要的,接下来几天都会尽自己所能尽可能将这方面讲清楚.  View的事件 ...

  3. 使用PIE对IE进行CSS3兼容介绍和经验总结

    国外团队开发的兼容插件,去年做项目时才发现,非常强大 主角:PIE.js , PIE.htc 两种方法可以实现 官方网站:http://css3pie.com/ 演示地址:http://css3pie ...

  4. DapperHelper,SqlHelper

    using System;using System.Collections.Generic;using System.Data.Common;using System.Linq;using Syste ...

  5. 【Sort】RadixSort基数排序

    太晚了,明天有时间在写算法思路,先贴代码 ------------------------------------------------ 刚答辩完,毕业好难,感觉自己好水 ------------- ...

  6. poj_3261_Milk Patterns(后缀数组)

    题目链接:poj_3261_Milk Patterns 题意: 给你一串数,让你找重复出现不少于k次的子串的最长长度,重复出现可重叠 题解: 老套路,还是二分答案,然后用height数组来check答 ...

  7. strrchr

    strrchr() 函数查找字符在指定字符串中从正面开始的最后一次出现的位置,如果成功,则返回从该位置到字符串结尾的所有字符,如果失败,则返回 false.与之相对应的是strchr()函数,它查找字 ...

  8. git clone 出现 RPC failed 错误的解决方案

    今天使用git clone一个大型项目的时候出现了如下错误:

  9. erlang四大behaviour之四-supervisor

    http://www.cnblogs.com/puputu/articles/1689621.html 1. 监督规则 一个监督者负责启动.停止.监控他的子进程.监督者的一个基本概念就是当必要的时候重 ...

  10. ADO.NET基础、数据增删改查

    ADO.NET:数据访问技术,就是将C#和MSSQL连接起来的一个纽带.我们可以通过ADO.NET将内存中的临时数据写入到数据库中,也可以将数据库中的数据提取到内存中供程序调用. 数据库数据的增.删. ...