Blame


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