in reply to Learning classes in Perl
Hi ravi45722
You are a little mixed up; you've copied code snippets meant to illustrate different things and patched them together, but the result doesn't make sense.
Your package will contain the constructor sub new() and your script that uses the package will call new() to create an instance of the object.
Also, even though that tutorial shows package 'File' as an example, it's customary to avoid single-word package names unless they are distinctive; you would be safer with either MyFile or My::File.
Update in response to OP's reply:
Are you sure you're ready to tackle OOP in Perl? Have you already become familiar with packages in general? Here's a simple example of a package in a module and a script that uses it, download it and try it out:
# 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
Usage:#!/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
Now if you wanted to do the same thing with OOP, making an object for your day, you might do something like this:
# 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
Usage:#!/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 obj +ect, # which contains your mood stored with +in it. if ( $prediction ) { say 'Your day will ' . $prediction . ', do not worry.'; } else { say 'Cannot read your mood so unable to predict your day.'; } __END__
Hope this is a little clearer now!$ perl my_day.pl sad
Update: Changed 'package must return a true value' to 'module must return a true value', thanks Discipulus.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Learning classes in Perl
by jdporter (Paladin) on Feb 20, 2016 at 01:48 UTC | |
by Anonymous Monk on Feb 20, 2016 at 02:26 UTC | |
by jdporter (Paladin) on Feb 20, 2016 at 16:48 UTC | |
|
Re^2: Learning classes in Perl
by ravi45722 (Pilgrim) on Feb 19, 2016 at 04:18 UTC | |
by poj (Abbot) on Feb 19, 2016 at 09:38 UTC |