Perl Getopt::Long命令行参数传递

这篇博客介绍了在Perl中如何处理命令行参数。通过使用Getopt::Std和Getopt::Long模块,可以方便地解析简单和复杂的命令行选项。一个例子展示了如何从命令行获取用户的年龄,并根据年龄执行不同的操作。当输入的参数不符合预定义的选项时,脚本会终止执行。
摘要由CSDN通过智能技术生成

原文链接:https://www.javatpoint.com/perl-command-line-arguments

The simple command line options are done using ?s option. Complex command line options are done using Getopt::Std and Getopt::Long.

Getopt stands for GetOptions. It processes the content of @ARGV based on the configuration we give to it. It returns true or false value based on the processing.

Example:

In this example, we well get the age of the user from the command line.

use strict;  
use warnings;  
use 5.010;  
use Getopt::Long qw(GetOptions);  
my $x;  
GetOptions('from=s' => \$x) or die "Usage: $0 --from NAME\n";  
say"According to your age which is $x:";  
if ($x >=18) {  
   say "You are eligible to vote";  
}   

We have declared a variable $x which will store value inserted into --from from the standard console. The from=s declares command line parameter called --from with a string after it. It is mapped further to the variable $x. The backslash () means we are passing a reference to the variable.

This script will switch to die part only when we will run this script by passing something that looks like a parameter name and starts with a (-) but is not declared in this script.

Now, we will run this program.

Output:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值