乱读天书, 不求甚解
周祎骏的个人云笔记
Toggle navigation
乱读天书, 不求甚解
主页
Linux:系统配置
Linux:用户管理
Linux:优化排错
Linux:进程调度
Linux:文件系统
Linux:网络
Linux:系统服务
Linux:安全
Linux:内核
容器:Docker
容器:containerd
容器编排:Kubernetes
IAC:Terraform
大数据:Hadoop
大数据:Zookeeper
大数据:Hbase
消息队列:rsyslog
消息队列:kafka
数据库:MySQL
数据库:MongoDB
搜索引擎:Elasticsearch
时序数据库:OpenTSDB
网站服务:Nginx
编程:Bash
编程:Perl
编程:Python
编程:C
编程:JAVA
编程:Rust
版本控制:gitlab
知识管理:docusaurus
常用小工具
关于我
标签
perl 3.04 用模块获得复杂的参数
2016-05-26 15:00:18
51
0
0
admin
>写脚本经常要处理传递的参数(@ARGV),bash 有getopts 可以用,perl有Getopt::Std或者Getopt::Long可以用。 #Getopt::Std ```perl [leo@localhost perl]$ cat getopt_std.pl #!/usr/bin/perl use warnings; use strict; use Getopt::Std; use vars qw( $opt_a $opt_b $opt_c ); getopts('a:b:c'); # 有冒号表示后面跟参数,没冒号表示不需要参数 print "a=$opt_a;b=$opt_b;c=$opt_c;\n"; [leo@localhost perl]$ ./getopt_std.pl -a d -b g -c o a=d;b=g;c=1; ``` *** #Getopt::Long ```perl [leo@localhost perl]$ cat getopt_long.pl #!/usr/bin/perl use warnings; use strict; use Getopt::Long; use Data::Dumper; my ( $a, $b, $c, $d, $e ); my @array; my %hash; GetOptions( 'help|h=s' => \$a, # -help,-h 都赋值给$a, "=s" 的“=" 表示后面必须跟参数 'int=i' => \$b, # “=s" 表示参数是字符串,i 表示int ,f 表示浮点数 'str:s' => \$c, # ":" 表示后面可跟参数也可以不跟 'number+' => \$d, # "+" 会赋值给$d "-number" 出现的次数 'yes_or_no!' => \$e, # "!" 表示该选项不需要参数 'lib=s' => \@array, # 会把 -lib 后的东西加入@array 数组 'hash=s' => \%hash, # 会把-hash 后的东西加入%hash(键=值) ); print "a=$a;b=$b;c=$c,d=$d;e=$e\n"; print Dumper @array; print Dumper %hash; [leo@localhost perl]$ ./getopt_long.pl -h a -int 2 -str -number -number -yes_or_no -lib lala -lib rr -hash l=r -hash o=t a=a;b=2;c=,d=2;e=1 $VAR1 = 'lala'; $VAR2 = 'rr'; $VAR1 = 'l'; $VAR2 = 'r'; $VAR3 = 'o'; $VAR4 = 't'; [leo@localhost perl]$ ```
上一篇:
perl 3.03 用URI::Escape处理URI 中的特殊字符
下一篇:
perl 3.05 连数据库的方式(mysql)
文档导航