[Kc] Perl Noob

David Nicol davidnicol at gmail.com
Wed Sep 19 13:46:37 PDT 2007


>  #!/usr/bin/perl
>
>  use warnings;
>  use strict;
>
>  print join("\n",
>    map({"$_ is " .
>      (exists($ENV{$_}) ?
>        (defined($ENV{$_}) ? $ENV{$_} : '-undef-') :
>        '-non-existent-'
>      )
>    } @ARGV)
>  ), "\n";
>
> --Eric

Or, getting more obscure for no reason,

#!/usr/bin/perl

 use warnings;
 use strict;

 $" = "\n";
 my @expanded_list =
    map({"$_ is " .
      (exists($ENV{$_}) ?
        (defined($ENV{$_}) ? $ENV{$_} : '-undef-') :
        '-non-existent-'
      )
    } sort @ARGV;

 print "@expanded_list\n";

__END__

there isn't a really tight way to use hash slice syntax, because of
the "$key is ..." bit

this is a lot clearer, while just skipping the missing ones; and using
the default
variable instead of an explicit $key:

for (sort @ARGV){
    exists $ENV{$_} or next;
    print "$_ is $ENV{$_}\n";
};
__END__

which can be made one line using postfix for

    exists $ENV{$_} and print "$_ is $ENV{$_}\n" for (sort @ARGV);

blah> perl -le 'exists $ENV{$_} and print "$_ is $ENV{$_}" for (sort
@ARGV)' USER SHELL HOME BOGUS
HOME is /home/david
SHELL is /bin/crash
USER is david
blah>


More information about the kc mailing list