perl + 确定参数是否为来自 ARG 的空值 [英] perl + identify if param is empty value from ARG

查看:49
本文介绍了perl + 确定参数是否为来自 ARG 的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不带参数运行以下 script.pl 脚本时:

when I run the following script.pl script with no arguments:

./script.pl

我没有收到消息No arg.为什么?如何识别 $param 是空值还是空值,和 ksh 中的 [ -z 一样?

I do not get the message No arg. Why? How to identify if $param is a null value or empty value, same as [ -z from ksh?

#!/usr/bin/perl
my $param = $ARGV[0];
if ($param = "") {
    print No arg;
} else {
    print arg: $param;
}

推荐答案

因为它不是 Perl.你从哪里学的那个语法?它有很多问题.

Because it's not Perl. Where did you learn that syntax? So much is wrong with it.

  1. $param = ""$param 分配一个空字符串,这不是你想要的.

  1. $param = "" assigns an empty string to $param, that's not what you want.

null 拼写为 undef 在 Perl 中.

null is spelled undef in Perl.

要比较字符串,请使用 eq 运算符.

To compare strings, use the eq operator.

您必须引用字符串:打印无参数"

更容易:

#!/usr/bin/perl
if (@ARGV) {
    print 'have parameters';
} else {
    print q{don't have parameters};
}

这篇关于perl + 确定参数是否为来自 ARG 的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆