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

Let's say I want to print out environment variables like this:

print "PATH is $ENV{'PATH'}\n";
Sometimes an env variable might not be set. And instead of printing empty space when undefined, I would like the undefined variable to say "undefined". So in the above example, I would get:

PATH is undefined.

Perhaps one possible solution is to do a check before the print statement such as:

$ENV{'PATH'} = "undefined" unless defined($ENV{'PATH'});

But I don't like this solution because in my program I will be printing out many environment variables which may or may not be defined. I would have to add a large block of code consisting of lines like the one above for each variable I want to print out. Is there an easier way to do this?

UPDATE: One thing I forgot to mention is that I am actually using a heredoc to print.

And see pg's response as to why what I am attempting to do is dangerous.

Replies are listed 'Best First'.
Re: Setting values for undefined environment variables
by pg (Canon) on Oct 24, 2004 at 03:19 UTC

    Not a good idea.

    The PURPOSE is to print "undefined" on screen, but not to change the value of the environment variable to "undefined", which could be a valid value. The right way is, when you print, check whether the environment variable is defined first, if not, say something like "blah is not defined", else say "blah = blah".

    print 'FOO is ', $ENV{'FOO'} ? $ENV{'FOO'} : 'not defined';

    Don't do more than you should. If 30 days down the road, someone else, or even yourself want to modify the code, and need to check whether the variable is set, what he or you will do, most likely will check whether it is undef, but what the hell, the value is not undef as it supposed to be, it is defined, and has the value "undefined". Someone could kill you.

      ++ Great point.

      print <<"EOT"; The environment variable foo is: @{[ $ENV{'foo'} ? $ENV{'foo'} : 'not defined' ]} That is all. EOT
Re: Setting values for undefined environment variables
by Limbic~Region (Chancellor) on Oct 24, 2004 at 02:37 UTC
    jacques,
    Since there could be a whole bunch:
    sub ENV { defined $ENV{$_[0]} ? $ENV{$_[0]} : 'undefined' } print "$_ is ", ENV($_), "\n" for qw(many environment variables);

    Cheers - L~R

      Thanks, but one problem with this approach is that I am actually using a heredoc to print. I didn't say this in my OP. I better add an update.

        And that's a problem why?

        ... print <<"EOT"; The environment variable $foo is: @{[ ENV( $foo ) ]} That is all. EOT ...
Re: Setting values for undefined environment variables
by kvale (Monsignor) on Oct 24, 2004 at 02:35 UTC
    my @vars = qw|PATH TEXLIB|; foreach my $var (@vars) { $ENV{$var} = "undefined" unless defined $ENV{$var}; }

    -Mark

      Thanks. That's much better.
Re: Setting values for undefined environment variables
by mhearse (Chaplain) on Oct 24, 2004 at 08:16 UTC
    I would do this
    #!/usr/bin/perl @wanted = qw(BAD OSTYPE TZ TERM); foreach $val (@wanted) { if ($ENV{$val}) { print "$val \t is \t $ENV{$val}\n"; } else { print "$val \t is \t undefined\n"; } }
    GIVES:
    BAD is undefined OSTYPE is OpenBSD TZ is undefined TERM is xterm