http://qs321.pair.com?node_id=11142352

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

if ($ARGV[0] ne "") { $destdir = $ARGV[0]; } else { if (exists $ENV{'LOGS_EV'}) { $destdir = $ENV{'LOGS_EV'}; } else { colored_printout(RED,"\nERROR: Environment variable LOGS_EV not +defined!"); } }

Here the above code prints the statement in the last else loop like "ERROR: Environment variable LOGS_EV not defined!". But i want the code to store the some value in the $destdir for this what changes can i do.

Replies are listed 'Best First'.
Re: In my perl code i am getting some other output instead of Other
by haj (Vicar) on Mar 24, 2022 at 10:35 UTC

    The error message appears if you neither provide an argument nor a value for the environment variable LOGS_EV. Which value would you like to store to $destdir in that case? Do you have a default value hidden somewhere? If so, consider:

    $destdir = $ARGV[0] // $ENV{'LOGS_EV'} // $some_value;
Re: In my perl code i am getting some other output instead of Other
by Corion (Patriarch) on Mar 24, 2022 at 10:26 UTC

    Maybe you want to use a similar line to assign a value to $destdir as you use in the other cases?

    This sounds to me like a very basic programming question - maybe you want to review your course material on how to assign values?

Re: In my perl code i am getting some other output instead of Other
by graff (Chancellor) on Mar 25, 2022 at 00:31 UTC
    When writing a script for command-line use, I try to make it easy for the user to get enough information about the expected / allowed args and options. Given your very limited example, I would tend to do it like this:
    my $usage = "Usage: $0 [path]\n default: value of env.variable LOGS_EV +\n"; my $destdir; if ( @ARGV ) { $destdir = $ARGV[0]; } elsif ( $ENV{'LOGS_EV'} ) { $destdir = $ENV{'LOGS_EV'} } else { warn "Please supply a path or set LOGS_EV\n\n"; die $usage; }