#!/usr/bin/perl -w

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
# final_3.pl
# 
# assignment: given a data structure, write subroutines to describe
#             that structure and to add to that structure
#           
#
# Author: John Berry
# Date: 12/11/03
#
# Version 0.01
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  

use strict;

my $VERSION = "0.01";

# 'person' data structure
my $person = {
              name => "Jim",
              age => 44,
              brothers => [ 'Matt', 'Bill', 'Paul' ],
              sisters => [ 'Marie', 'Annie', 'Aileen' ]
             };

# these are just the calls to the routines so we can see how they are affected
summarize($person);
add_brother($person, 'Joe');
summarize($person);

# # # # # # # # # # # # # # SUBROUTINES # # # # # # # # # # # # # # # # # # # # 

# name:   summarize
# effect: prints a summary of a person structure
# usage:  summarize($person); 
# note:   $person must be a reference to a data structure
sub summarize {
  my $person = shift;

  # print name and age
  print("The person '", $person->{name}, "' is ", $person->{age}, " years old.\n");

  # print brothers
  print("Brothers are ");
  foreach my $brother (@{$person->{brothers}}) {

    # we use this if to determine if we are on the last brother in the list, and if so
    # we end with a period instead of a comma
    if($brother ne $person->{brothers}[(scalar(@{$person->{brothers}}) - 1)]) {
      print("$brother, ");
    } else {
      print("$brother.\n");
    }
  }

  # print sisters  
  print("Sisters are ");
  foreach my $sister (@{$person->{sisters}}) {

    # we use this if to determine if we are on the last brother in the list, and if so
    # we end with a period instead of a comma
    if($sister ne $person->{sisters}[(scalar(@{$person->{sisters}}) - 1)]) {
      print("$sister, ");
    } else {
      print("$sister.\n\n");
    }
  }
}


# name:   add_brother
# effect: adds a new brother to a 'person' structure
# usage:  add_brother($person, $new_brother)  
# note:   $person must be a reference to a 'person' data structure
sub add_brother {
  my $person = shift;
  my $new_brother = shift;
  print("\n\n$new_brother\n\n");
  push(@{$person->{brothers}}, ($new_brother));
}

