package Emoticon; #----------------------------------------------------------------------------- # PACKAGE: Emoticon.pm # PURPOSE: To encapsulate a emoticon (e.g., :-) and illustrate basic # object-oriented programming with Perl 5. # AUTHOR: Steve A. Chervitz (sac@genome.stanford.edu) # SOURCE: http://genome-www.stanford.edu/~sac/perlOOP/examples/ (defunct) # MODIFIED: cdl -- Thu Feb 14 2008 #----------------------------------------------------------------------------- # CLASS CONSTANTS: my $DEFAULT_FACE = ":-)"; # DATA MEMBERS: # eyes : single character to represent eyes. # nose : single character to represent nose. # mouth : single character to represent mouth. # CONSTRUCTOR: sub new { my ($class, $face) = @_; # Create the anonymous hash reference to hold the object's data. my $self = {}; # Initialize the emoticon data members. $face = $DEFAULT_FACE unless defined $face; ($self->{eyes}, $self->{nose}, $self->{mouth}) = split(//, $face, 3); # Return the hash reference blessed into the current package. return bless $self, $class; } # ACCESSORS: # Here we are using combined set/get accessors for all data members, # since these data are simple. The "set" portions of these accessors # could do some data validation, to prevent bogus emoticons. sub eyes { my $self = shift; if (@_) { $self->{eyes} = shift; } return $self->{eyes}; } sub nose { my $self = shift; if (@_) { $self->{nose} = shift; } return $self->{nose}; } sub mouth { my $self = shift; if (@_) { $self->{mouth} = shift; } return $self->{mouth}; } # Get the whole emoticon: sub face { my $self = shift; return $self->{eyes} . $self->{nose} . $self->{mouth}; } # INSTANCE METHODS: sub frown { my $self = shift; $self->{mouth} = "("; # Return the object reference to permit method chaining as in: # $obj->frown->face return $self; } sub print { my $self = shift; return print $self->face(); } # This ensures this package evaluates true by the Perl interpreter. ":-)"; __END__ ;-( Angry 8-O Astonished %+{ Beat up =^D Big grin |-D Big laugh ?-( Black eye %-6 Brain-dead :-) Classic Smile :*) Clowning %-( Confused :,( Crying %-) Dazed 8-# Death :-6 Exhausted :-( Frown :-i Half-smile :-> Happiness :^D Happy %-} Humorous %-\ Hung over :-| Indifferent %*} Inebriated %-{ Ironic :-* Kiss =^* Kisses :-D Laughing :-9 Lips, licking :/i No smoking :/) Not funny :-j One-sided smile 8-[ Overwrought #-) Partied all night :-I Pondering :+( Punched in the nose :-k Puzzlement :-] Sarcasm :-@ Screaming :-x Sealed lips M-) See no evil 8-o Shocked *-) Shot to death :-V Shouting :~) Sick, from a cold :-Y Side comment :-} Smile, mischievous :-, Smirk |^o Snoring :-M Speak no evil :-P Sticking out tongue |-( Struggling to stay awake :-O Surprised :-? Tongue in cheek :-& Tongue-tied :-[ Unsmiling; also criticism :-< Very sad :-# Wearing braces 8-) Wearing glasses 8-| Wide-eyed surprise ;-D Winking and laughing ;-) Winky %-| Worked all night 8-] Wow! :-/ Wry face }-) Wry smile |-O Yawn