# DayPredict.pm # maps moods to predictions package DayPredict; use strict; use warnings; sub predict { my $mood = shift; if ( $mood eq 'happy' ) { return 'be great'; } elsif ( $mood eq 'sad' ) { return 'get better'; } else { return; # didn't get a mood so return undef } } 1; # module must return a true value #### #!/usr/bin/perl # predict_day.pl # Tells what sort of day you will have based on your mood use strict; use warnings; use feature 'say'; use DayPredict; my $mood = $ARGV[0] or die "no mood!"; my $prediction = DayPredict::predict( $mood ); if ( $prediction ) { say 'Your day will ' . $prediction . ', do not worry.'; } else { say 'Cannot read your mood so unable to predict your day.'; } __END__ #### $ perl predict_day.pl happy #### # MyDay.pm # Interface to objects representing days package MyDay; use strict; use warnings; sub new { # This is boilerplate code you can copy. # But you'd be better off with a module so you don't have to do it by hand. # Check out Class::Tiny or Moo my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless ($self, $class); return $self; } sub mood { my $self = shift; # If there's an argument provided, set the value, # then in either case, return the current value. if ( @_ ) { $self->{'mood'} = shift; } return $self->{'mood'}; } sub predict { my $self = shift; my $prediction; # Get the mood by calling the mood() method on your object # ... referred to as $self here while you're inside it if ( $self->mood eq 'happy' ) { $prediction = 'be great'; } elsif ( $self->mood eq 'sad' ) { $prediction = 'get better'; } return $prediction; # undef if no mood } 1; # module must return a true value #### #!/usr/bin/perl use strict; use warnings; use feature 'say'; # my_day.pl - predicts your day based on your mood use MyDay; my $mood = $ARGV[0] or die "no mood!"; my $day = MyDay->new; # construct a new object $day->mood( $mood ); # set the value of the object attribute # based on this script's argument my $prediction = $day->predict; # call the predict() method on the object, # which contains your mood stored within it. if ( $prediction ) { say 'Your day will ' . $prediction . ', do not worry.'; } else { say 'Cannot read your mood so unable to predict your day.'; } __END__ #### $ perl my_day.pl sad