IT 441: Network Services Administration
Class #06 Exercise

Complete by: 2/27/2018


Log in to users.cs.umb.edu

Practice with reading from a file

(If you do not have a .bashrc file in your home directory, then just substitute some other text file in your home directory. Or, really, any absolute path to a text file will do.)
perl>  open ( INA , '<' , '/home/your_username/.bashrc' ) or die $!;

                        Replace your_username with your actual username on users.cs.umb.edu

perl>  while (<INA>) { print "$_" ; }

perl>  close INA;
(What happens if you use ~ instead of typing out the absolute path to your home?)
(What happens if you just use the filename -- such as .bashrc -- on its own, without the absolute path?)

Practice with writing to a file

perl>  open ( OUT1 , '>' , './test_out.txt' ) or die $!;

perl>  print OUT1 "foo\n";

perl>  print OUT1 "bar\n";

perl>  print OUT1 "blech\n";

perl>  close (OUT1);

Now, let's look at the file's contents...

perl>  open ( IN1 , '<' , './test_out.txt' ) or die $!;

perl>  print <IN1>;

perl>  close (IN1);
(How is using the code print <IN1>; different from printing the file's contents using a while loop? Is it any different, in terms of the resulting output?)

Writing to a pre-existing file

perl>  open ( OUT2 , '>' , './test_out.txt' ) or die $!;

perl>  print OUT2 "first\n";

perl>  print OUT2 "second\n";

perl>  print OUT2 "third\n";

perl>  print OUT2 "fourth\n";

perl>  close (OUT2);

Look at the file's contents...

perl>  open ( IN2 , '<' , './test_out.txt' ) or die $!;

perl>  while (<IN2>) { chomp $_ ; print "$_\n" ; }

perl>  close IN2;
(What happened?)
(Is any part of our while loop redundant? What part and why or why not?)

Appending to a pre-existing file

perl>  open ( OUT3 , '>>' , './test_out.txt' ) or die $!;

perl>  print OUT3 "5th\n";

perl>  print OUT3 "6th\n";

perl>  print OUT3 "7th\n";

perl>  print OUT3 "8th\n";

perl>  close (OUT3);

Look at the file's contents...

perl>  open ( IN3 , '<' , './test_out.txt' ) or die $!;

perl>  while (<IN3>) { chomp $_ ; print "$_\n" ; }

perl>  close IN3;
(What happened?)
(Is any part of our while loop redundant? What part and why or why not?)

End the Perl Session

perl>  exit

End the script session

Use the exit command. You should then have a file called typescript in your current working directory.