IT 441: Network Services Administration
Class #02 Exercise

Complete by: 2/6/2018


Purpose

The purpose of this exercise is to set you up for using Perl on the Linux server. You will probably also want Perl on your own computer, if it's not already there. If you do so, make sure you are getting Perl 5, specifically; you should get a version number close to that which we recorded in Exercise #01.

As you go through this exercise, you are going to need to keep track of

Log in to users.cs.umb.edu

For the parts that require you to edit a file, you have either of two options:
  1. Open a separate login shell window into users to do your file editing, so that you don't disrupt your script session.
  2. You can exit your your script session, and then start up a new one while adding to the pre-existing typescript file by using the command script -a. This requires that you be in the same directory as the typescript file.
The first option -- like we did in Exercise #01 -- is probably the easiest because you don't have to worry about remembering the -a when you re-run the script command.

Configure CPAN

This will be within your script session...
You will need to configure the cpan utility. The Comprehensive Perl Archive Network (CPAN) allows you to acquire new modules for Perl, in order to expand its capabilities. To configure, enter the following command:

cpan

You will receive a couple of prompts, to each of which you may simply press Enter:

Would you like to configure as much as possible automatically? [yes]
What approach do you want? (Choose 'local::lib', 'sudo' or 'manual')
[local::lib]
You will receive the following message:
local::lib is installed. You must now add the following environment variables
to your shell configuration files (or registry, if you are on Windows) and
then restart your command line shell and CPAN before installing modules:
There will be five lines of shell code for defining/exporting environment variables. They will look like this:
PATH="/home/your_username/perl5/bin${PATH:+:${PATH}}"; export PATH;
PERL5LIB="/home/your_username/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/your_username/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/your_username/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/your_username/perl5"; export PERL_MM_OPT;
Copy these lines from the given output so that you can later add them to your shell config files -- .bash_profile and/or .bashrc -- and re-source them. There may also be an option to add these to your .bashrc file automatically!

You will then see the CPAN shell started up with prompts that look like this: cpan[1]> The prompts' numbers will increment consecutively -- 1, 2, 3...

This puts you in the CPAN shell, which you can think of as a subshell within the cpan process you started up.

You will need to input the following commands at the prompt. (Note that the .... indicates command output.)
cpan[1]>  install CPAN

....

cpan[2]>  reload cpan

....

cpan[3]>  install Try::Tiny

....

cpan[4]>  h

....

cpan[5]>  q

....
The h command gives you the help menu for the CPAN shell, while the q command lets you exit the CPAN shell.
Quitting the CPAN shell puts you back where you were in your script session, which is taking place in a subshell of your main login shell.

Edit Startup File

This is where you will need to work in another shell, as indicated above. Do not do the file editing within your script session!
Earlier, you were asked to copy some lines of shell code printed out during CPAN configuration. Open your .bashrc file for editing
                nano $HOME/.bashrc
and add the lines of code. In addition, add the following line to your .bashrc file:

alias perlsession='$HOME/it441/re.pl'

Save the file.

Create a REPL script for Perl

Now, we will set up a makeshift REPL (read-eval-print loop) in Perl. In your it441 directory, create a file called re.pl

                nano $HOME/it441/re.pl
and add to it the following text:
#! /usr/bin/perl

use Try::Tiny;

$| = 1;

while(true) {
    print "perl>  ";
    $code=<>;
    try {
        eval($code);
        print "\n";
        # $error=$@;
    } catch {
        warn "caught error: $_";
    };
    print "\n";
}

Use chmod to make the script executable:

chmod +x $HOME/it441/re.pl

Back to your script session...
Confirm you are within your it441/ex/ex02 directory:

pwd

You need to "source" your .bashrc file:

source $HOME/.bashrc

Use the alias you created to run the REPL:

perlsession

You will get a prompt like this: perl>

Try out some of the following lines at the prompt:

perl>  print "Hello, world!";

perl>  print 2 + 4;

perl>  print "Number: " . 6;

perl>  exit

End the script session

Use the exit command. Ending your script session puts you back in your main login shell... You should then have a file called typescript in your current working directory -- which should be your ex02 directory.

You may also wish to take the lines you added to your .bashrc file and put them in your .bash_profile file, as well, so that they will be added to the environment when you enter a login shell, too. This includes the line that defines the perlsession alias.