#!/usr/bin/perl -w
##################
#
# inclass.pl
# looking at writing pig latin done in class.
#
# input from STDIN; output to STDOUT
# usage:  ./inclass.pl < input.txt > output.txt
#
# See 225Lab3.htm for the piglatin rules.
#
# version history
#   0.1  read in from standard input, write to standard output
#        while splitting up words
#   0.2  add 'way' after words that start with aeiou
#        see inclass_v2_input.txt and inclass_v2_output.txt
#   0.3  move stuff after first usable vowel to end; append 'ay'
#        tested with loop over some sample words
#   0.4  added capitalization 
#   
#
###################
use strict;
my $VERSION = 0.4;
print " --- inclass.pl - version $VERSION --- \n";

# -- Testing v0.3 consonant_swap subroutine. --
# for my $word ( 'constant', 'crybaby', 'sheep' ){
#  my $swapword  =consonant_swap($word);
#  print " swap($word) is '$swapword' \n";
# }
# exit;

# -- Testing v0.4 capitlization
# for my $word ( 'ConsTant', 'Crybaby', 'sheep', 'STicky', 'MacLeish' ){
#  my $swapword  =consonant_swap($word);
#  print " original is '$word'; consonant_swap is '$swapword' \n";
# }
# exit;

my @words = split /\b/, join('',<STDIN>);

# -- this was for debuggin version 0.1  --
# my $count = 0;
# foreach my $word (@words){
#  $count++;
#  print "word $count: '$word'\n";
# }

foreach my $word (@words){
  if     ( $word =~ /^[aeiou]/i){        # if word starts with vowel,
    $word = $word . 'way';               #   append 'way'
  }
  elsif  ( $word =~ /^[a-z]/i){          # word starts with some letter
    $word = consonant_swap($word);
  }
  print $word;
}

# given a single word that doesn't begin with a vowel,
# put consonants at end and append 'ay'.  Return the word.
# usage: $word = consonant_swap("hello");  # gives 'ellohay'
sub consonant_swap {
  my ($word) = @_;
  if ($word =~ /^(.[^aeiouy]*)([aeiouy])(.*)$/i){
    #          $1=1st_char    $2=vowel  $3=after
    # only do this swap if we do match that pattern.
    my $one   = $1 || '';
    my $two   = $2 || '';
    my $three = $3 || '';
    if ($one =~ /^[A-Z]/){
      $two = ucfirst($two);
      $one = lcfirst($one);
    }
    $word = "$two$three$one"."ay";
  }
  return $word;
}


