Blob


1 #!/usr/bin/perl
3 # Modify the program from Exercise 3 in Chapter 6 (the environment
4 # lister) to print (undefined value) for environment variables
5 # without a value. You can set the new environment variables in the
6 # program. Ensure that your program reports the right thing for
7 # variables with a false value. If you are using Perl 5.10 or
8 # later, use the // operator. Otherwise, use the conditional
9 # operator.
11 #!/usr/bin/perl
13 #Write a program to list all of the keys and values in %ENV. Print the
14 #results in two columns in ASCIIbetical order. For extra credit, arrange the
15 #output to vertically align both columns. The length function can help you
16 #figure out how wide to make the first column. Once you get the program
17 #running, try setting some new environment variables and ensuring that they
18 #show up in your output.
20 use v5.10;
21 use warnings;
22 use strict;
23 use utf8;
24 use Data::Dumper;
26 my $maxlen;
27 $ENV{"UNDEF"} = undef;
28 $ENV{"ZERO"} = 0;
29 $ENV{"EMPTY"} = "";
30 foreach my $key (sort(keys %ENV)) {
31 if (length($key) > $maxlen) {
32 $maxlen = length($key);
33 }
34 }
35 foreach my $key (sort(keys %ENV)) {
36 printf "%${maxlen}s => %s\n", $key, $ENV{$key} // "(undefined value)";
37 }