Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl + identify if param is empty value from ARG

Tags:

perl

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

./script.pl

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;
}
like image 612
lidia Avatar asked Feb 18 '26 15:02

lidia


1 Answers

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

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

  2. null is spelled undef in Perl.

  3. To compare strings, use the eq operator.

  4. You must quote strings: print "No arg"

Much easier:

#!/usr/bin/perl
if (@ARGV) {
    print 'have parameters';
} else {
    print q{don't have parameters};
}
like image 81
daxim Avatar answered Feb 21 '26 12:02

daxim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!