in reply to Make command line argument optional

Hello TonyNY,

You should always code with

use strict; use warnings;

at the head of your script, in which case, if $ARGV[2] is undefined, the statement chomp $id; will generate a warning. So the correct logic is:

use strict; use warnings; my ($pkgname, $envname, $id) = @ARGV; if (defined $id) { chomp $id; print "Third argument: >$id<\n"; } else { print "No third argument\n"; }

BTW, these statements:

chomp; s/^\s+|\s+$//g; # trim spaces

operate on the default variable $_, which is not initialised in the code snippet you show.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Make command line argument optional
by TonyNY (Beadle) on Jul 10, 2018 at 20:20 UTC

    I was ale to accomplish what I need to do by using the following. Thanks for your help.

    my $id = $ARGV[2]; if (not defined $id) { print "$id is not defined"; } else { print "$id is defined"; }

      I suspect you want

      if (not defined $id) { print '$id is not defined';
      Notice the change in quotes, inside double quotes $id will try to interpolate, and it is undefined