http://www.perlmonks.org?node_id=889915


in reply to Re: Not quite an OO tutorial
in thread Not quite an OO tutorial

Since GrandFather wrote the object oriented code above for me, I am more than willing to show how I would write it in functions. I let my eyes glaze over on the overdrive bit, so I don't have a functional way to do that (if there is a way, that is).

Let's start with the module:

packack Random::Things; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw(RandDayPart RandLight RandWeather RandTense); sub RandDayPart { my @randdaypart = qw(morning evening afternoon night); return $randdaypart[rand @randdaypart]; } sub RandLight { my @randlight = qw(bright dark gloomy dazzling); return $randlight[rand @randlight]; } sub RandWeather { my @randweather = qw(stormy windy rainy calm); return $randweather[rand @randweather]; } sub RandTense { my @randtense = ('was', 'is', 'will be'); return $randtense[rand @randtense]; } 1;

Now the script:

use strict; use warnings; use Random::Things qw(RandDayPart RandLight RandWeather RandTense); my $random_day_part = RandDayPart; my $random_light = RandLight; my $random_weather = RandWeather; my $random_tense = RandTense; print "It $random_tense a $random_light and $random_weather $random_da +y_part."

It may not be nice and have things like inheritance that the OO version has, but it will do the same thing in a crunch.

Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^3: Not quite an OO tutorial
by james2vegas (Chaplain) on Feb 24, 2011 at 06:37 UTC
    I would skip the duplication and the exporting of functions, something like this:

    package Rand; use strict; use warnings; sub _random { my @values = @_; return $values[rand @values]; } sub day_part { return _random(qw(morning evening afternoon night)); } sub light { return _random(qw(bright dark gloomy dazzling)); } sub weather { return _random(qw(stormy windy rainy calm)); } sub tense { return _random('was', 'is', 'will be'); } 1;

    and the example code would look like this:
    use strict; use warnings; use Rand; my $random_day_part = Rand::day_part; my $random_light = Rand::light; my $random_weather = Rand::weather; my $random_tense = Rand::tense; print "It $random_tense a $random_light and $random_weather $random_da +y_part."

      Until I read your post, james2vegas, I thought that the only want to get a subroutine out of a module without use base 'Exporter'; was to use objects which is why I always use base 'Exporter'. I have not seen subroutines constructed like that before. Would you please tell me what that is called (and maybe where I can find it) so that I can do more reading on it? There is more to non-object oriented programming that I don't know which may be why I am having a hard time grasping object oriented programming in the first place. Maybe the first thing I need to do is to get away from the Exporter without objects before getting into objects. I have been tied to Exporter since Lady Aleena's first working module.

      Have a cookie and a very nice day!
      Lady Aleena

        Lady_Aleena, my knowledge is pretty basic still, but for what it's worth I found the information here and page 9 here helpful to understand more about how the subs are working. Maybe this will be helpful to you as well.

      I found information on the constructs that you used, james2vegas, that confused me in Exporter under "Selecting What To Export."

      1. A leading underscore on a subroutine name (_someSub) is just a convention to denote subroutines that are internal and are to be used only in the module.
      2. ModuleName::someSub is just another way to call a subroutine without having to export it explicitly using our @EXPORT or our @EXPORT_OK.

      These things, while minor, are nice to know for my future writings. I will start adding leading underscores to my internal subroutines, since it is a great visual aid. I might also start using ModuleName::someSub when a module of mine has only one or two subroutines. Thanks for showing me these shortcuts! Cookies! :)

      Have a cookie and a very nice day!
      Lady Aleena