Perl :- List System Environment Variable

Background

Yo Bro!

This Open Source stuff is killing me.

Having to install a whole bunch of stuff on my laptop.

Things I will only use for a season.

Tool-set

There is a toolset that I need to see if it is even worth familiarizing with.

Will be easy if it were written in a modern programming language.

But, no it is Perl.

Artifacts

Downloaded ActiveState Perl from here.

Tool

Before using the tool, I feel a need to at minimum write a “Hello World” application in Perl and make sure my environment is good.

Code

Display Username from OS Environment Variable

Perl – Snippet


 #!/usr/bin/perl
use strict;
use warnings;

my $username;
my $format;

$username =  $ENV{'USERNAME'}; 

$format = "Who is learning Perl Today ? $username\n"; 

print $format; 

Output

helloWorld.01.20190806.0632PM

 

List Environment Variables


#!/usr/bin/perl
use strict;
use warnings;

my $username;
my $format;
my %listofEnv;
my $iNumberofElements=0;
my $key;

$username =  $ENV{'USERNAME'}; 

$format = "Who is learning Perl Today ? $username\n"; 

print $format; 

# capture environment hash table in local hash
# Copying a hash to another hash problem
# https://www.perlmonks.org/?node_id=207083
%listofEnv=%ENV;

#Length of an array in Perl
#https://perlmaven.com/length-of-an-array
$iNumberofElements = scalar(%listofEnv);

#Display Number of elements
$format = "Number of elements in environment $iNumberofElements \n";
print $format; 

# transverse collection
foreach my $key (keys (%listofEnv) )
{

    # get value of listofEnv => key
    $format = "$key = $listofEnv{$key} \n\n";

    print $format;

}

Source Code Control

GitHub

DanielAdeniji\perlSample
Link

Summary

It is not a big deal.

But, if I am going to evaluate someone’s toolset, I should at least know a bit about the constraints he worked\walked within.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s